diff options
Diffstat (limited to 'src/com/android/launcher2')
39 files changed, 11289 insertions, 0 deletions
diff --git a/src/com/android/launcher2/AddAdapter.java b/src/com/android/launcher2/AddAdapter.java new file mode 100644 index 0000000..a6c889e --- /dev/null +++ b/src/com/android/launcher2/AddAdapter.java @@ -0,0 +1,112 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.content.Context; +import android.content.res.Resources; +import android.graphics.drawable.Drawable; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.TextView; + +import java.util.ArrayList; + +/** + * Adapter showing the types of items that can be added to a {@link Workspace}. + */ +public class AddAdapter extends BaseAdapter { + + private final LayoutInflater mInflater; + + private final ArrayList<ListItem> mItems = new ArrayList<ListItem>(); + + public static final int ITEM_SHORTCUT = 0; + public static final int ITEM_APPWIDGET = 1; + public static final int ITEM_LIVE_FOLDER = 2; + public static final int ITEM_WALLPAPER = 3; + + /** + * Specific item in our list. + */ + public class ListItem { + public final CharSequence text; + public final Drawable image; + public final int actionTag; + + public ListItem(Resources res, int textResourceId, int imageResourceId, int actionTag) { + text = res.getString(textResourceId); + if (imageResourceId != -1) { + image = res.getDrawable(imageResourceId); + } else { + image = null; + } + this.actionTag = actionTag; + } + } + + public AddAdapter(Launcher launcher) { + super(); + + mInflater = (LayoutInflater) launcher.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + + // Create default actions + Resources res = launcher.getResources(); + + mItems.add(new ListItem(res, R.string.group_shortcuts, + R.drawable.ic_launcher_shortcut, ITEM_SHORTCUT)); + + mItems.add(new ListItem(res, R.string.group_widgets, + R.drawable.ic_launcher_appwidget, ITEM_APPWIDGET)); + + mItems.add(new ListItem(res, R.string.group_live_folders, + R.drawable.ic_launcher_folder_live, ITEM_LIVE_FOLDER)); + + mItems.add(new ListItem(res, R.string.group_wallpapers, + R.drawable.ic_launcher_gallery, ITEM_WALLPAPER)); + + } + + public View getView(int position, View convertView, ViewGroup parent) { + ListItem item = (ListItem) getItem(position); + + if (convertView == null) { + convertView = mInflater.inflate(R.layout.add_list_item, parent, false); + } + + TextView textView = (TextView) convertView; + textView.setTag(item); + textView.setText(item.text); + textView.setCompoundDrawablesWithIntrinsicBounds(item.image, null, null, null); + + return convertView; + } + + public int getCount() { + return mItems.size(); + } + + public Object getItem(int position) { + return mItems.get(position); + } + + public long getItemId(int position) { + return position; + } + +} diff --git a/src/com/android/launcher2/AllAppsGridView.java b/src/com/android/launcher2/AllAppsGridView.java new file mode 100644 index 0000000..b8f7902 --- /dev/null +++ b/src/com/android/launcher2/AllAppsGridView.java @@ -0,0 +1,125 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.widget.GridView; +import android.widget.AdapterView; +import android.content.Context; +import android.content.res.TypedArray; +import android.util.AttributeSet; +import android.view.View; +import android.graphics.BitmapFactory; +import android.graphics.Bitmap; +import android.graphics.Paint; +import android.graphics.Canvas; + +public class AllAppsGridView extends GridView implements AdapterView.OnItemClickListener, + AdapterView.OnItemLongClickListener, DragSource { + + private DragController mDragger; + private Launcher mLauncher; + private Bitmap mTexture; + private Paint mPaint; + private int mTextureWidth; + private int mTextureHeight; + + public AllAppsGridView(Context context) { + super(context); + } + + public AllAppsGridView(Context context, AttributeSet attrs) { + this(context, attrs, com.android.internal.R.attr.gridViewStyle); + } + + public AllAppsGridView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AllAppsGridView, defStyle, 0); + final int textureId = a.getResourceId(R.styleable.AllAppsGridView_texture, 0); + if (textureId != 0) { + mTexture = BitmapFactory.decodeResource(getResources(), textureId); + mTextureWidth = mTexture.getWidth(); + mTextureHeight = mTexture.getHeight(); + + mPaint = new Paint(); + mPaint.setDither(false); + } + a.recycle(); + } + + @Override + protected void onFinishInflate() { + setOnItemClickListener(this); + setOnItemLongClickListener(this); + } + + @Override + public void draw(Canvas canvas) { + final Bitmap texture = mTexture; + final Paint paint = mPaint; + + final int width = getWidth(); + final int height = getHeight(); + + final int textureWidth = mTextureWidth; + final int textureHeight = mTextureHeight; + + int x = 0; + int y; + + while (x < width) { + y = 0; + while (y < height) { + canvas.drawBitmap(texture, x, y, paint); + y += textureHeight; + } + x += textureWidth; + } + + super.draw(canvas); + } + + public void onItemClick(AdapterView parent, View v, int position, long id) { + ApplicationInfo app = (ApplicationInfo) parent.getItemAtPosition(position); + mLauncher.startActivitySafely(app.intent); + } + + public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { + if (!view.isInTouchMode()) { + return false; + } + + ApplicationInfo app = (ApplicationInfo) parent.getItemAtPosition(position); + app = new ApplicationInfo(app); + + mDragger.startDrag(view, this, app, DragController.DRAG_ACTION_COPY); + mLauncher.closeAllApplications(); + + return true; + } + + public void setDragger(DragController dragger) { + mDragger = dragger; + } + + public void onDropCompleted(View target, boolean success) { + } + + void setLauncher(Launcher launcher) { + mLauncher = launcher; + } +} diff --git a/src/com/android/launcher2/ApplicationInfo.java b/src/com/android/launcher2/ApplicationInfo.java new file mode 100644 index 0000000..cee9f3b --- /dev/null +++ b/src/com/android/launcher2/ApplicationInfo.java @@ -0,0 +1,127 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.content.ComponentName; +import android.content.ContentValues; +import android.content.Intent; +import android.graphics.Bitmap; +import android.graphics.drawable.Drawable; + +/** + * Represents a launchable application. An application is made of a name (or title), + * an intent and an icon. + */ +class ApplicationInfo extends ItemInfo { + + /** + * The application name. + */ + CharSequence title; + + /** + * The intent used to start the application. + */ + Intent intent; + + /** + * The application icon. + */ + Drawable icon; + + /** + * When set to true, indicates that the icon has been resized. + */ + boolean filtered; + + /** + * Indicates whether the icon comes from an application's resource (if false) + * or from a custom Bitmap (if true.) + */ + boolean customIcon; + + /** + * If isShortcut=true and customIcon=false, this contains a reference to the + * shortcut icon as an application's resource. + */ + Intent.ShortcutIconResource iconResource; + + ApplicationInfo() { + itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT; + } + + public ApplicationInfo(ApplicationInfo info) { + super(info); + title = info.title.toString(); + intent = new Intent(info.intent); + if (info.iconResource != null) { + iconResource = new Intent.ShortcutIconResource(); + iconResource.packageName = info.iconResource.packageName; + iconResource.resourceName = info.iconResource.resourceName; + } + icon = info.icon; + filtered = info.filtered; + customIcon = info.customIcon; + } + + /** + * Creates the application intent based on a component name and various launch flags. + * Sets {@link #itemType} to {@link LauncherSettings.BaseLauncherColumns#ITEM_TYPE_APPLICATION}. + * + * @param className the class name of the component representing the intent + * @param launchFlags the launch flags + */ + final void setActivity(ComponentName className, int launchFlags) { + intent = new Intent(Intent.ACTION_MAIN); + intent.addCategory(Intent.CATEGORY_LAUNCHER); + intent.setComponent(className); + intent.setFlags(launchFlags); + itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION; + } + + @Override + void onAddToDatabase(ContentValues values) { + super.onAddToDatabase(values); + + String titleStr = title != null ? title.toString() : null; + values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr); + + String uri = intent != null ? intent.toUri(0) : null; + values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri); + + if (customIcon) { + values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE, + LauncherSettings.BaseLauncherColumns.ICON_TYPE_BITMAP); + Bitmap bitmap = ((FastBitmapDrawable) icon).getBitmap(); + writeBitmap(values, bitmap); + } else { + values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE, + LauncherSettings.BaseLauncherColumns.ICON_TYPE_RESOURCE); + if (iconResource != null) { + values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE, + iconResource.packageName); + values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE, + iconResource.resourceName); + } + } + } + + @Override + public String toString() { + return title.toString(); + } +} diff --git a/src/com/android/launcher2/ApplicationsAdapter.java b/src/com/android/launcher2/ApplicationsAdapter.java new file mode 100644 index 0000000..97891b2 --- /dev/null +++ b/src/com/android/launcher2/ApplicationsAdapter.java @@ -0,0 +1,58 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.TextView; + +import java.util.ArrayList; + +/** + * GridView adapter to show the list of applications and shortcuts + */ +public class ApplicationsAdapter extends ArrayAdapter<ApplicationInfo> { + private final LayoutInflater mInflater; + + public ApplicationsAdapter(Context context, ArrayList<ApplicationInfo> apps) { + super(context, 0, apps); + mInflater = LayoutInflater.from(context); + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + final ApplicationInfo info = getItem(position); + + if (convertView == null) { + convertView = mInflater.inflate(R.layout.application_boxed, parent, false); + } + + if (!info.filtered) { + info.icon = Utilities.createIconThumbnail(info.icon, getContext()); + info.filtered = true; + } + + final TextView textView = (TextView) convertView; + textView.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null); + textView.setText(info.title); + + return convertView; + } +} diff --git a/src/com/android/launcher2/BubbleTextView.java b/src/com/android/launcher2/BubbleTextView.java new file mode 100644 index 0000000..3782454 --- /dev/null +++ b/src/com/android/launcher2/BubbleTextView.java @@ -0,0 +1,134 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.widget.TextView; +import android.content.Context; +import android.util.AttributeSet; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.RectF; +import android.graphics.drawable.Drawable; +import android.text.Layout; + +/** + * TextView that draws a bubble behind the text. We cannot use a LineBackgroundSpan + * because we want to make the bubble taller than the text and TextView's clip is + * too aggressive. + */ +public class BubbleTextView extends TextView { + private static final float CORNER_RADIUS = 8.0f; + private static final float PADDING_H = 5.0f; + private static final float PADDING_V = 1.0f; + + private final RectF mRect = new RectF(); + private Paint mPaint; + + private boolean mBackgroundSizeChanged; + private Drawable mBackground; + private float mCornerRadius; + private float mPaddingH; + private float mPaddingV; + + public BubbleTextView(Context context) { + super(context); + init(); + } + + public BubbleTextView(Context context, AttributeSet attrs) { + super(context, attrs); + init(); + } + + public BubbleTextView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + init(); + } + + private void init() { + setFocusable(true); + mBackground = getBackground(); + setBackgroundDrawable(null); + mBackground.setCallback(this); + + mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); + mPaint.setColor(getContext().getResources().getColor(R.color.bubble_dark_background)); + + final float scale = getContext().getResources().getDisplayMetrics().density; + mCornerRadius = CORNER_RADIUS * scale; + mPaddingH = PADDING_H * scale; + //noinspection PointlessArithmeticExpression + mPaddingV = PADDING_V * scale; + } + + @Override + protected boolean setFrame(int left, int top, int right, int bottom) { + if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) { + mBackgroundSizeChanged = true; + } + return super.setFrame(left, top, right, bottom); + } + + @Override + protected boolean verifyDrawable(Drawable who) { + return who == mBackground || super.verifyDrawable(who); + } + + @Override + protected void drawableStateChanged() { + Drawable d = mBackground; + if (d != null && d.isStateful()) { + d.setState(getDrawableState()); + } + super.drawableStateChanged(); + } + + @Override + public void draw(Canvas canvas) { + final Drawable background = mBackground; + if (background != null) { + final int scrollX = mScrollX; + final int scrollY = mScrollY; + + if (mBackgroundSizeChanged) { + background.setBounds(0, 0, mRight - mLeft, mBottom - mTop); + mBackgroundSizeChanged = false; + } + + if ((scrollX | scrollY) == 0) { + background.draw(canvas); + } else { + canvas.translate(scrollX, scrollY); + background.draw(canvas); + canvas.translate(-scrollX, -scrollY); + } + } + + final Layout layout = getLayout(); + final RectF rect = mRect; + final int left = getCompoundPaddingLeft(); + final int top = getExtendedPaddingTop(); + + rect.set(left + layout.getLineLeft(0) - mPaddingH, + top + layout.getLineTop(0) - mPaddingV, + Math.min(left + layout.getLineRight(0) + mPaddingH, mScrollX + mRight - mLeft), + top + layout.getLineBottom(0) + mPaddingV); + canvas.drawRoundRect(rect, mCornerRadius, mCornerRadius, mPaint); + + super.draw(canvas); + } +} diff --git a/src/com/android/launcher2/CellLayout.java b/src/com/android/launcher2/CellLayout.java new file mode 100644 index 0000000..73dbb3e --- /dev/null +++ b/src/com/android/launcher2/CellLayout.java @@ -0,0 +1,1033 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.Rect; +import android.graphics.RectF; +import android.util.AttributeSet; +import android.view.ContextMenu; +import android.view.MotionEvent; +import android.view.View; +import android.view.ViewDebug; +import android.view.ViewGroup; + +import java.util.ArrayList; + +public class CellLayout extends ViewGroup { + private boolean mPortrait; + + private int mCellWidth; + private int mCellHeight; + + private int mLongAxisStartPadding; + private int mLongAxisEndPadding; + + private int mShortAxisStartPadding; + private int mShortAxisEndPadding; + + private int mShortAxisCells; + private int mLongAxisCells; + + private int mWidthGap; + private int mHeightGap; + + private final Rect mRect = new Rect(); + private final CellInfo mCellInfo = new CellInfo(); + + int[] mCellXY = new int[2]; + + boolean[][] mOccupied; + + private RectF mDragRect = new RectF(); + + private boolean mDirtyTag; + + public CellLayout(Context context) { + this(context, null); + } + + public CellLayout(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public CellLayout(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0); + + mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10); + mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10); + + mLongAxisStartPadding = + a.getDimensionPixelSize(R.styleable.CellLayout_longAxisStartPadding, 10); + mLongAxisEndPadding = + a.getDimensionPixelSize(R.styleable.CellLayout_longAxisEndPadding, 10); + mShortAxisStartPadding = + a.getDimensionPixelSize(R.styleable.CellLayout_shortAxisStartPadding, 10); + mShortAxisEndPadding = + a.getDimensionPixelSize(R.styleable.CellLayout_shortAxisEndPadding, 10); + + mShortAxisCells = a.getInt(R.styleable.CellLayout_shortAxisCells, 4); + mLongAxisCells = a.getInt(R.styleable.CellLayout_longAxisCells, 4); + + a.recycle(); + + setAlwaysDrawnWithCacheEnabled(false); + + if (mOccupied == null) { + if (mPortrait) { + mOccupied = new boolean[mShortAxisCells][mLongAxisCells]; + } else { + mOccupied = new boolean[mLongAxisCells][mShortAxisCells]; + } + } + } + + @Override + public void cancelLongPress() { + super.cancelLongPress(); + + // Cancel long press for all children + final int count = getChildCount(); + for (int i = 0; i < count; i++) { + final View child = getChildAt(i); + child.cancelLongPress(); + } + } + + int getCountX() { + return mPortrait ? mShortAxisCells : mLongAxisCells; + } + + int getCountY() { + return mPortrait ? mLongAxisCells : mShortAxisCells; + } + + @Override + public void addView(View child, int index, ViewGroup.LayoutParams params) { + // Generate an id for each view, this assumes we have at most 256x256 cells + // per workspace screen + final LayoutParams cellParams = (LayoutParams) params; + child.setId(((getId() & 0xFF) << 16) | + (cellParams.cellX & 0xFF) << 8 | (cellParams.cellY & 0xFF)); + + super.addView(child, index, params); + } + + @Override + public void requestChildFocus(View child, View focused) { + super.requestChildFocus(child, focused); + if (child != null) { + Rect r = new Rect(); + child.getDrawingRect(r); + requestRectangleOnScreen(r); + } + } + + @Override + protected void onAttachedToWindow() { + super.onAttachedToWindow(); + mCellInfo.screen = ((ViewGroup) getParent()).indexOfChild(this); + } + + @Override + public boolean onInterceptTouchEvent(MotionEvent ev) { + final int action = ev.getAction(); + final CellInfo cellInfo = mCellInfo; + + if (action == MotionEvent.ACTION_DOWN) { + final Rect frame = mRect; + final int x = (int) ev.getX() + mScrollX; + final int y = (int) ev.getY() + mScrollY; + final int count = getChildCount(); + + boolean found = false; + for (int i = count - 1; i >= 0; i--) { + final View child = getChildAt(i); + + if ((child.getVisibility()) == VISIBLE || child.getAnimation() != null) { + child.getHitRect(frame); + if (frame.contains(x, y)) { + final LayoutParams lp = (LayoutParams) child.getLayoutParams(); + cellInfo.cell = child; + cellInfo.cellX = lp.cellX; + cellInfo.cellY = lp.cellY; + cellInfo.spanX = lp.cellHSpan; + cellInfo.spanY = lp.cellVSpan; + cellInfo.valid = true; + found = true; + mDirtyTag = false; + break; + } + } + } + + if (!found) { + int cellXY[] = mCellXY; + pointToCellExact(x, y, cellXY); + + final boolean portrait = mPortrait; + final int xCount = portrait ? mShortAxisCells : mLongAxisCells; + final int yCount = portrait ? mLongAxisCells : mShortAxisCells; + + final boolean[][] occupied = mOccupied; + findOccupiedCells(xCount, yCount, occupied, null); + + cellInfo.cell = null; + cellInfo.cellX = cellXY[0]; + cellInfo.cellY = cellXY[1]; + cellInfo.spanX = 1; + cellInfo.spanY = 1; + cellInfo.valid = cellXY[0] >= 0 && cellXY[1] >= 0 && cellXY[0] < xCount && + cellXY[1] < yCount && !occupied[cellXY[0]][cellXY[1]]; + + // Instead of finding the interesting vacant cells here, wait until a + // caller invokes getTag() to retrieve the result. Finding the vacant + // cells is a bit expensive and can generate many new objects, it's + // therefore better to defer it until we know we actually need it. + + mDirtyTag = true; + } + setTag(cellInfo); + } else if (action == MotionEvent.ACTION_UP) { + cellInfo.cell = null; + cellInfo.cellX = -1; + cellInfo.cellY = -1; + cellInfo.spanX = 0; + cellInfo.spanY = 0; + cellInfo.valid = false; + mDirtyTag = false; + setTag(cellInfo); + } + + return false; + } + + @Override + public CellInfo getTag() { + final CellInfo info = (CellInfo) super.getTag(); + if (mDirtyTag && info.valid) { + final boolean portrait = mPortrait; + final int xCount = portrait ? mShortAxisCells : mLongAxisCells; + final int yCount = portrait ? mLongAxisCells : mShortAxisCells; + + final boolean[][] occupied = mOccupied; + findOccupiedCells(xCount, yCount, occupied, null); + + findIntersectingVacantCells(info, info.cellX, info.cellY, xCount, yCount, occupied); + + mDirtyTag = false; + } + return info; + } + + private static void findIntersectingVacantCells(CellInfo cellInfo, int x, int y, + int xCount, int yCount, boolean[][] occupied) { + + cellInfo.maxVacantSpanX = Integer.MIN_VALUE; + cellInfo.maxVacantSpanXSpanY = Integer.MIN_VALUE; + cellInfo.maxVacantSpanY = Integer.MIN_VALUE; + cellInfo.maxVacantSpanYSpanX = Integer.MIN_VALUE; + cellInfo.clearVacantCells(); + + if (occupied[x][y]) { + return; + } + + cellInfo.current.set(x, y, x, y); + + findVacantCell(cellInfo.current, xCount, yCount, occupied, cellInfo); + } + + private static void findVacantCell(Rect current, int xCount, int yCount, boolean[][] occupied, + CellInfo cellInfo) { + + addVacantCell(current, cellInfo); + + if (current.left > 0) { + if (isColumnEmpty(current.left - 1, current.top, current.bottom, occupied)) { + current.left--; + findVacantCell(current, xCount, yCount, occupied, cellInfo); + current.left++; + } + } + + if (current.right < xCount - 1) { + if (isColumnEmpty(current.right + 1, current.top, current.bottom, occupied)) { + current.right++; + findVacantCell(current, xCount, yCount, occupied, cellInfo); + current.right--; + } + } + + if (current.top > 0) { + if (isRowEmpty(current.top - 1, current.left, current.right, occupied)) { + current.top--; + findVacantCell(current, xCount, yCount, occupied, cellInfo); + current.top++; + } + } + + if (current.bottom < yCount - 1) { + if (isRowEmpty(current.bottom + 1, current.left, current.right, occupied)) { + current.bottom++; + findVacantCell(current, xCount, yCount, occupied, cellInfo); + current.bottom--; + } + } + } + + private static void addVacantCell(Rect current, CellInfo cellInfo) { + CellInfo.VacantCell cell = CellInfo.VacantCell.acquire(); + cell.cellX = current.left; + cell.cellY = current.top; + cell.spanX = current.right - current.left + 1; + cell.spanY = current.bottom - current.top + 1; + if (cell.spanX > cellInfo.maxVacantSpanX) { + cellInfo.maxVacantSpanX = cell.spanX; + cellInfo.maxVacantSpanXSpanY = cell.spanY; + } + if (cell.spanY > cellInfo.maxVacantSpanY) { + cellInfo.maxVacantSpanY = cell.spanY; + cellInfo.maxVacantSpanYSpanX = cell.spanX; + } + cellInfo.vacantCells.add(cell); + } + + private static boolean isColumnEmpty(int x, int top, int bottom, boolean[][] occupied) { + for (int y = top; y <= bottom; y++) { + if (occupied[x][y]) { + return false; + } + } + return true; + } + + private static boolean isRowEmpty(int y, int left, int right, boolean[][] occupied) { + for (int x = left; x <= right; x++) { + if (occupied[x][y]) { + return false; + } + } + return true; + } + + CellInfo findAllVacantCells(boolean[] occupiedCells, View ignoreView) { + final boolean portrait = mPortrait; + final int xCount = portrait ? mShortAxisCells : mLongAxisCells; + final int yCount = portrait ? mLongAxisCells : mShortAxisCells; + + boolean[][] occupied = mOccupied; + + if (occupiedCells != null) { + for (int y = 0; y < yCount; y++) { + for (int x = 0; x < xCount; x++) { + occupied[x][y] = occupiedCells[y * xCount + x]; + } + } + } else { + findOccupiedCells(xCount, yCount, occupied, ignoreView); + } + + CellInfo cellInfo = new CellInfo(); + + cellInfo.cellX = -1; + cellInfo.cellY = -1; + cellInfo.spanY = 0; + cellInfo.spanX = 0; + cellInfo.maxVacantSpanX = Integer.MIN_VALUE; + cellInfo.maxVacantSpanXSpanY = Integer.MIN_VALUE; + cellInfo.maxVacantSpanY = Integer.MIN_VALUE; + cellInfo.maxVacantSpanYSpanX = Integer.MIN_VALUE; + cellInfo.screen = mCellInfo.screen; + + Rect current = cellInfo.current; + + for (int x = 0; x < xCount; x++) { + for (int y = 0; y < yCount; y++) { + if (!occupied[x][y]) { + current.set(x, y, x, y); + findVacantCell(current, xCount, yCount, occupied, cellInfo); + occupied[x][y] = true; + } + } + } + + cellInfo.valid = cellInfo.vacantCells.size() > 0; + + // Assume the caller will perform their own cell searching, otherwise we + // risk causing an unnecessary rebuild after findCellForSpan() + + return cellInfo; + } + + /** + * Given a point, return the cell that strictly encloses that point + * @param x X coordinate of the point + * @param y Y coordinate of the point + * @param result Array of 2 ints to hold the x and y coordinate of the cell + */ + void pointToCellExact(int x, int y, int[] result) { + final boolean portrait = mPortrait; + + final int hStartPadding = portrait ? mShortAxisStartPadding : mLongAxisStartPadding; + final int vStartPadding = portrait ? mLongAxisStartPadding : mShortAxisStartPadding; + + result[0] = (x - hStartPadding) / (mCellWidth + mWidthGap); + result[1] = (y - vStartPadding) / (mCellHeight + mHeightGap); + + final int xAxis = portrait ? mShortAxisCells : mLongAxisCells; + final int yAxis = portrait ? mLongAxisCells : mShortAxisCells; + + if (result[0] < 0) result[0] = 0; + if (result[0] >= xAxis) result[0] = xAxis - 1; + if (result[1] < 0) result[1] = 0; + if (result[1] >= yAxis) result[1] = yAxis - 1; + } + + /** + * Given a point, return the cell that most closely encloses that point + * @param x X coordinate of the point + * @param y Y coordinate of the point + * @param result Array of 2 ints to hold the x and y coordinate of the cell + */ + void pointToCellRounded(int x, int y, int[] result) { + pointToCellExact(x + (mCellWidth / 2), y + (mCellHeight / 2), result); + } + + /** + * Given a cell coordinate, return the point that represents the upper left corner of that cell + * + * @param cellX X coordinate of the cell + * @param cellY Y coordinate of the cell + * + * @param result Array of 2 ints to hold the x and y coordinate of the point + */ + void cellToPoint(int cellX, int cellY, int[] result) { + final boolean portrait = mPortrait; + + final int hStartPadding = portrait ? mShortAxisStartPadding : mLongAxisStartPadding; + final int vStartPadding = portrait ? mLongAxisStartPadding : mShortAxisStartPadding; + + + result[0] = hStartPadding + cellX * (mCellWidth + mWidthGap); + result[1] = vStartPadding + cellY * (mCellHeight + mHeightGap); + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + // TODO: currently ignoring padding + + int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); + int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); + + int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); + int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); + + if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) { + throw new RuntimeException("CellLayout cannot have UNSPECIFIED dimensions"); + } + + final int shortAxisCells = mShortAxisCells; + final int longAxisCells = mLongAxisCells; + final int longAxisStartPadding = mLongAxisStartPadding; + final int longAxisEndPadding = mLongAxisEndPadding; + final int shortAxisStartPadding = mShortAxisStartPadding; + final int shortAxisEndPadding = mShortAxisEndPadding; + final int cellWidth = mCellWidth; + final int cellHeight = mCellHeight; + + mPortrait = heightSpecSize > widthSpecSize; + + int numShortGaps = shortAxisCells - 1; + int numLongGaps = longAxisCells - 1; + + if (mPortrait) { + int vSpaceLeft = heightSpecSize - longAxisStartPadding - longAxisEndPadding + - (cellHeight * longAxisCells); + mHeightGap = vSpaceLeft / numLongGaps; + + int hSpaceLeft = widthSpecSize - shortAxisStartPadding - shortAxisEndPadding + - (cellWidth * shortAxisCells); + if (numShortGaps > 0) { + mWidthGap = hSpaceLeft / numShortGaps; + } else { + mWidthGap = 0; + } + } else { + int hSpaceLeft = widthSpecSize - longAxisStartPadding - longAxisEndPadding + - (cellWidth * longAxisCells); + mWidthGap = hSpaceLeft / numLongGaps; + + int vSpaceLeft = heightSpecSize - shortAxisStartPadding - shortAxisEndPadding + - (cellHeight * shortAxisCells); + if (numShortGaps > 0) { + mHeightGap = vSpaceLeft / numShortGaps; + } else { + mHeightGap = 0; + } + } + + int count = getChildCount(); + + for (int i = 0; i < count; i++) { + View child = getChildAt(i); + LayoutParams lp = (LayoutParams) child.getLayoutParams(); + + if (mPortrait) { + lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap, shortAxisStartPadding, + longAxisStartPadding); + } else { + lp.setup(cellWidth, cellHeight, mWidthGap, mHeightGap, longAxisStartPadding, + shortAxisStartPadding); + } + + int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY); + int childheightMeasureSpec = + MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY); + child.measure(childWidthMeasureSpec, childheightMeasureSpec); + } + + setMeasuredDimension(widthSpecSize, heightSpecSize); + } + + @Override + protected void onLayout(boolean changed, int l, int t, int r, int b) { + int count = getChildCount(); + + for (int i = 0; i < count; i++) { + View child = getChildAt(i); + if (child.getVisibility() != GONE) { + + CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); + + int childLeft = lp.x; + int childTop = lp.y; + child.layout(childLeft, childTop, childLeft + lp.width, childTop + lp.height); + } + } + } + + @Override + protected void setChildrenDrawingCacheEnabled(boolean enabled) { + final int count = getChildCount(); + for (int i = 0; i < count; i++) { + final View view = getChildAt(i); + view.setDrawingCacheEnabled(enabled); + // Update the drawing caches + view.buildDrawingCache(true); + } + } + + @Override + protected void setChildrenDrawnWithCacheEnabled(boolean enabled) { + super.setChildrenDrawnWithCacheEnabled(enabled); + } + + /** + * Find a vacant area that will fit the given bounds nearest the requested + * cell location. Uses Euclidean distance to score multiple vacant areas. + * + * @param pixelX The X location at which you want to search for a vacant area. + * @param pixelY The Y location at which you want to search for a vacant area. + * @param spanX Horizontal span of the object. + * @param spanY Vertical span of the object. + * @param vacantCells Pre-computed set of vacant cells to search. + * @param recycle Previously returned value to possibly recycle. + * @return The X, Y cell of a vacant area that can contain this object, + * nearest the requested location. + */ + int[] findNearestVacantArea(int pixelX, int pixelY, int spanX, int spanY, + CellInfo vacantCells, int[] recycle) { + + // Keep track of best-scoring drop area + final int[] bestXY = recycle != null ? recycle : new int[2]; + final int[] cellXY = mCellXY; + double bestDistance = Double.MAX_VALUE; + + // Bail early if vacant cells aren't valid + if (!vacantCells.valid) { + return null; + } + + // Look across all vacant cells for best fit + final int size = vacantCells.vacantCells.size(); + for (int i = 0; i < size; i++) { + final CellInfo.VacantCell cell = vacantCells.vacantCells.get(i); + + // Reject if vacant cell isn't our exact size + if (cell.spanX != spanX || cell.spanY != spanY) { + continue; + } + + // Score is center distance from requested pixel + cellToPoint(cell.cellX, cell.cellY, cellXY); + + double distance = Math.sqrt(Math.pow(cellXY[0] - pixelX, 2) + + Math.pow(cellXY[1] - pixelY, 2)); + if (distance <= bestDistance) { + bestDistance = distance; + bestXY[0] = cell.cellX; + bestXY[1] = cell.cellY; + } + } + + // Return null if no suitable location found + if (bestDistance < Double.MAX_VALUE) { + return bestXY; + } else { + return null; + } + } + + /** + * Drop a child at the specified position + * + * @param child The child that is being dropped + * @param targetXY Destination area to move to + */ + void onDropChild(View child, int[] targetXY) { + LayoutParams lp = (LayoutParams) child.getLayoutParams(); + lp.cellX = targetXY[0]; + lp.cellY = targetXY[1]; + lp.isDragging = false; + mDragRect.setEmpty(); + child.requestLayout(); + invalidate(); + } + + void onDropAborted(View child) { + if (child != null) { + ((LayoutParams) child.getLayoutParams()).isDragging = false; + invalidate(); + } + mDragRect.setEmpty(); + } + + /** + * Start dragging the specified child + * + * @param child The child that is being dragged + */ + void onDragChild(View child) { + LayoutParams lp = (LayoutParams) child.getLayoutParams(); + lp.isDragging = true; + mDragRect.setEmpty(); + } + + /** + * Drag a child over the specified position + * + * @param child The child that is being dropped + * @param cellX The child's new x cell location + * @param cellY The child's new y cell location + */ + void onDragOverChild(View child, int cellX, int cellY) { + int[] cellXY = mCellXY; + pointToCellRounded(cellX, cellY, cellXY); + LayoutParams lp = (LayoutParams) child.getLayoutParams(); + cellToRect(cellXY[0], cellXY[1], lp.cellHSpan, lp.cellVSpan, mDragRect); + invalidate(); + } + + /** + * Computes a bounding rectangle for a range of cells + * + * @param cellX X coordinate of upper left corner expressed as a cell position + * @param cellY Y coordinate of upper left corner expressed as a cell position + * @param cellHSpan Width in cells + * @param cellVSpan Height in cells + * @param dragRect Rectnagle into which to put the results + */ + public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF dragRect) { + final boolean portrait = mPortrait; + final int cellWidth = mCellWidth; + final int cellHeight = mCellHeight; + final int widthGap = mWidthGap; + final int heightGap = mHeightGap; + + final int hStartPadding = portrait ? mShortAxisStartPadding : mLongAxisStartPadding; + final int vStartPadding = portrait ? mLongAxisStartPadding : mShortAxisStartPadding; + + int width = cellHSpan * cellWidth + ((cellHSpan - 1) * widthGap); + int height = cellVSpan * cellHeight + ((cellVSpan - 1) * heightGap); + + int x = hStartPadding + cellX * (cellWidth + widthGap); + int y = vStartPadding + cellY * (cellHeight + heightGap); + + dragRect.set(x, y, x + width, y + height); + } + + /** + * Computes the required horizontal and vertical cell spans to always + * fit the given rectangle. + * + * @param width Width in pixels + * @param height Height in pixels + */ + public int[] rectToCell(int width, int height) { + // Always assume we're working with the smallest span to make sure we + // reserve enough space in both orientations. + int actualWidth = mCellWidth + mWidthGap; + int actualHeight = mCellHeight + mHeightGap; + int smallerSize = Math.min(actualWidth, actualHeight); + + // Always round up to next largest cell + int spanX = (width + smallerSize) / smallerSize; + int spanY = (height + smallerSize) / smallerSize; + return new int[] { spanX, spanY }; + } + + /** + * Find the first vacant cell, if there is one. + * + * @param vacant Holds the x and y coordinate of the vacant cell + * @param spanX Horizontal cell span. + * @param spanY Vertical cell span. + * + * @return True if a vacant cell was found + */ + public boolean getVacantCell(int[] vacant, int spanX, int spanY) { + final boolean portrait = mPortrait; + final int xCount = portrait ? mShortAxisCells : mLongAxisCells; + final int yCount = portrait ? mLongAxisCells : mShortAxisCells; + final boolean[][] occupied = mOccupied; + + findOccupiedCells(xCount, yCount, occupied, null); + + return findVacantCell(vacant, spanX, spanY, xCount, yCount, occupied); + } + + static boolean findVacantCell(int[] vacant, int spanX, int spanY, + int xCount, int yCount, boolean[][] occupied) { + + for (int x = 0; x < xCount; x++) { + for (int y = 0; y < yCount; y++) { + boolean available = !occupied[x][y]; +out: for (int i = x; i < x + spanX - 1 && x < xCount; i++) { + for (int j = y; j < y + spanY - 1 && y < yCount; j++) { + available = available && !occupied[i][j]; + if (!available) break out; + } + } + + if (available) { + vacant[0] = x; + vacant[1] = y; + return true; + } + } + } + + return false; + } + + boolean[] getOccupiedCells() { + final boolean portrait = mPortrait; + final int xCount = portrait ? mShortAxisCells : mLongAxisCells; + final int yCount = portrait ? mLongAxisCells : mShortAxisCells; + final boolean[][] occupied = mOccupied; + + findOccupiedCells(xCount, yCount, occupied, null); + + final boolean[] flat = new boolean[xCount * yCount]; + for (int y = 0; y < yCount; y++) { + for (int x = 0; x < xCount; x++) { + flat[y * xCount + x] = occupied[x][y]; + } + } + + return flat; + } + + private void findOccupiedCells(int xCount, int yCount, boolean[][] occupied, View ignoreView) { + for (int x = 0; x < xCount; x++) { + for (int y = 0; y < yCount; y++) { + occupied[x][y] = false; + } + } + + int count = getChildCount(); + for (int i = 0; i < count; i++) { + View child = getChildAt(i); + if (child instanceof Folder || child.equals(ignoreView)) { + continue; + } + LayoutParams lp = (LayoutParams) child.getLayoutParams(); + + for (int x = lp.cellX; x < lp.cellX + lp.cellHSpan && x < xCount; x++) { + for (int y = lp.cellY; y < lp.cellY + lp.cellVSpan && y < yCount; y++) { + occupied[x][y] = true; + } + } + } + } + + @Override + public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { + return new CellLayout.LayoutParams(getContext(), attrs); + } + + @Override + protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { + return p instanceof CellLayout.LayoutParams; + } + + @Override + protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { + return new CellLayout.LayoutParams(p); + } + + public static class LayoutParams extends ViewGroup.MarginLayoutParams { + /** + * Horizontal location of the item in the grid. + */ + @ViewDebug.ExportedProperty + public int cellX; + + /** + * Vertical location of the item in the grid. + */ + @ViewDebug.ExportedProperty + public int cellY; + + /** + * Number of cells spanned horizontally by the item. + */ + @ViewDebug.ExportedProperty + public int cellHSpan; + + /** + * Number of cells spanned vertically by the item. + */ + @ViewDebug.ExportedProperty + public int cellVSpan; + + /** + * Is this item currently being dragged + */ + public boolean isDragging; + + // X coordinate of the view in the layout. + @ViewDebug.ExportedProperty + int x; + // Y coordinate of the view in the layout. + @ViewDebug.ExportedProperty + int y; + + public LayoutParams(Context c, AttributeSet attrs) { + super(c, attrs); + cellHSpan = 1; + cellVSpan = 1; + } + + public LayoutParams(ViewGroup.LayoutParams source) { + super(source); + cellHSpan = 1; + cellVSpan = 1; + } + + public LayoutParams(int cellX, int cellY, int cellHSpan, int cellVSpan) { + super(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); + this.cellX = cellX; + this.cellY = cellY; + this.cellHSpan = cellHSpan; + this.cellVSpan = cellVSpan; + } + + public void setup(int cellWidth, int cellHeight, int widthGap, int heightGap, + int hStartPadding, int vStartPadding) { + + final int myCellHSpan = cellHSpan; + final int myCellVSpan = cellVSpan; + final int myCellX = cellX; + final int myCellY = cellY; + + width = myCellHSpan * cellWidth + ((myCellHSpan - 1) * widthGap) - + leftMargin - rightMargin; + height = myCellVSpan * cellHeight + ((myCellVSpan - 1) * heightGap) - + topMargin - bottomMargin; + + x = hStartPadding + myCellX * (cellWidth + widthGap) + leftMargin; + y = vStartPadding + myCellY * (cellHeight + heightGap) + topMargin; + } + } + + static final class CellInfo implements ContextMenu.ContextMenuInfo { + /** + * See View.AttachInfo.InvalidateInfo for futher explanations about + * the recycling mechanism. In this case, we recycle the vacant cells + * instances because up to several hundreds can be instanciated when + * the user long presses an empty cell. + */ + static final class VacantCell { + int cellX; + int cellY; + int spanX; + int spanY; + + // We can create up to 523 vacant cells on a 4x4 grid, 100 seems + // like a reasonable compromise given the size of a VacantCell and + // the fact that the user is not likely to touch an empty 4x4 grid + // very often + private static final int POOL_LIMIT = 100; + private static final Object sLock = new Object(); + + private static int sAcquiredCount = 0; + private static VacantCell sRoot; + + private VacantCell next; + + static VacantCell acquire() { + synchronized (sLock) { + if (sRoot == null) { + return new VacantCell(); + } + + VacantCell info = sRoot; + sRoot = info.next; + sAcquiredCount--; + + return info; + } + } + + void release() { + synchronized (sLock) { + if (sAcquiredCount < POOL_LIMIT) { + sAcquiredCount++; + next = sRoot; + sRoot = this; + } + } + } + + @Override + public String toString() { + return "VacantCell[x=" + cellX + ", y=" + cellY + ", spanX=" + spanX + + ", spanY=" + spanY + "]"; + } + } + + View cell; + int cellX; + int cellY; + int spanX; + int spanY; + int screen; + boolean valid; + + final ArrayList<VacantCell> vacantCells = new ArrayList<VacantCell>(VacantCell.POOL_LIMIT); + int maxVacantSpanX; + int maxVacantSpanXSpanY; + int maxVacantSpanY; + int maxVacantSpanYSpanX; + final Rect current = new Rect(); + + void clearVacantCells() { + final ArrayList<VacantCell> list = vacantCells; + final int count = list.size(); + + for (int i = 0; i < count; i++) list.get(i).release(); + + list.clear(); + } + + void findVacantCellsFromOccupied(boolean[] occupied, int xCount, int yCount) { + if (cellX < 0 || cellY < 0) { + maxVacantSpanX = maxVacantSpanXSpanY = Integer.MIN_VALUE; + maxVacantSpanY = maxVacantSpanYSpanX = Integer.MIN_VALUE; + clearVacantCells(); + return; + } + + final boolean[][] unflattened = new boolean[xCount][yCount]; + for (int y = 0; y < yCount; y++) { + for (int x = 0; x < xCount; x++) { + unflattened[x][y] = occupied[y * xCount + x]; + } + } + CellLayout.findIntersectingVacantCells(this, cellX, cellY, xCount, yCount, unflattened); + } + + /** + * This method can be called only once! Calling #findVacantCellsFromOccupied will + * restore the ability to call this method. + * + * Finds the upper-left coordinate of the first rectangle in the grid that can + * hold a cell of the specified dimensions. + * + * @param cellXY The array that will contain the position of a vacant cell if such a cell + * can be found. + * @param spanX The horizontal span of the cell we want to find. + * @param spanY The vertical span of the cell we want to find. + * + * @return True if a vacant cell of the specified dimension was found, false otherwise. + */ + boolean findCellForSpan(int[] cellXY, int spanX, int spanY) { + return findCellForSpan(cellXY, spanX, spanY, true); + } + + boolean findCellForSpan(int[] cellXY, int spanX, int spanY, boolean clear) { + final ArrayList<VacantCell> list = vacantCells; + final int count = list.size(); + + boolean found = false; + + if (this.spanX >= spanX && this.spanY >= spanY) { + cellXY[0] = cellX; + cellXY[1] = cellY; + found = true; + } + + // Look for an exact match first + for (int i = 0; i < count; i++) { + VacantCell cell = list.get(i); + if (cell.spanX == spanX && cell.spanY == spanY) { + cellXY[0] = cell.cellX; + cellXY[1] = cell.cellY; + found = true; + break; + } + } + + // Look for the first cell large enough + for (int i = 0; i < count; i++) { + VacantCell cell = list.get(i); + if (cell.spanX >= spanX && cell.spanY >= spanY) { + cellXY[0] = cell.cellX; + cellXY[1] = cell.cellY; + found = true; + break; + } + } + + if (clear) clearVacantCells(); + + return found; + } + + @Override + public String toString() { + return "Cell[view=" + (cell == null ? "null" : cell.getClass()) + ", x=" + cellX + + ", y=" + cellY + "]"; + } + } +} + + diff --git a/src/com/android/launcher2/DeleteZone.java b/src/com/android/launcher2/DeleteZone.java new file mode 100644 index 0000000..02e8011 --- /dev/null +++ b/src/com/android/launcher2/DeleteZone.java @@ -0,0 +1,265 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.widget.ImageView; +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.Rect; +import android.util.AttributeSet; +import android.view.View; +import android.view.animation.TranslateAnimation; +import android.view.animation.Animation; +import android.view.animation.AnimationSet; +import android.view.animation.AccelerateInterpolator; +import android.view.animation.AlphaAnimation; +import android.graphics.RectF; +import android.graphics.drawable.TransitionDrawable; + +public class DeleteZone extends ImageView implements DropTarget, DragController.DragListener { + private static final int ORIENTATION_HORIZONTAL = 1; + private static final int TRANSITION_DURATION = 250; + private static final int ANIMATION_DURATION = 200; + + private final int[] mLocation = new int[2]; + + private Launcher mLauncher; + private boolean mTrashMode; + + private AnimationSet mInAnimation; + private AnimationSet mOutAnimation; + private Animation mHandleInAnimation; + private Animation mHandleOutAnimation; + + private int mOrientation; + private DragLayer mDragLayer; + + private final RectF mRegion = new RectF(); + private TransitionDrawable mTransition; + private View mHandle; + + public DeleteZone(Context context) { + super(context); + } + + public DeleteZone(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public DeleteZone(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DeleteZone, defStyle, 0); + mOrientation = a.getInt(R.styleable.DeleteZone_direction, ORIENTATION_HORIZONTAL); + a.recycle(); + } + + @Override + protected void onFinishInflate() { + super.onFinishInflate(); + mTransition = (TransitionDrawable) getBackground(); + } + + public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset, + Object dragInfo) { + return true; + } + + public Rect estimateDropLocation(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo, Rect recycle) { + return null; + } + + public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo) { + final ItemInfo item = (ItemInfo) dragInfo; + + if (item.container == -1) return; + + final LauncherModel model = Launcher.getModel(); + if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) { + if (item instanceof LauncherAppWidgetInfo) { + model.removeDesktopAppWidget((LauncherAppWidgetInfo) item); + } else { + model.removeDesktopItem(item); + } + } else { + if (source instanceof UserFolder) { + final UserFolder userFolder = (UserFolder) source; + final UserFolderInfo userFolderInfo = (UserFolderInfo) userFolder.getInfo(); + model.removeUserFolderItem(userFolderInfo, item); + } + } + if (item instanceof UserFolderInfo) { + final UserFolderInfo userFolderInfo = (UserFolderInfo)item; + LauncherModel.deleteUserFolderContentsFromDatabase(mLauncher, userFolderInfo); + model.removeUserFolder(userFolderInfo); + } else if (item instanceof LauncherAppWidgetInfo) { + final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item; + final LauncherAppWidgetHost appWidgetHost = mLauncher.getAppWidgetHost(); + if (appWidgetHost != null) { + appWidgetHost.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId); + } + } + LauncherModel.deleteItemFromDatabase(mLauncher, item); + } + + public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset, + Object dragInfo) { + mTransition.reverseTransition(TRANSITION_DURATION); + } + + public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset, + Object dragInfo) { + } + + public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset, + Object dragInfo) { + mTransition.reverseTransition(TRANSITION_DURATION); + } + + public void onDragStart(View v, DragSource source, Object info, int dragAction) { + final ItemInfo item = (ItemInfo) info; + if (item != null) { + mTrashMode = true; + createAnimations(); + final int[] location = mLocation; + getLocationOnScreen(location); + mRegion.set(location[0], location[1], location[0] + mRight - mLeft, + location[1] + mBottom - mTop); + mDragLayer.setDeleteRegion(mRegion); + mTransition.resetTransition(); + startAnimation(mInAnimation); + mHandle.startAnimation(mHandleOutAnimation); + setVisibility(VISIBLE); + } + } + + public void onDragEnd() { + if (mTrashMode) { + mTrashMode = false; + mDragLayer.setDeleteRegion(null); + startAnimation(mOutAnimation); + mHandle.startAnimation(mHandleInAnimation); + setVisibility(GONE); + } + } + + private void createAnimations() { + if (mInAnimation == null) { + mInAnimation = new FastAnimationSet(); + final AnimationSet animationSet = mInAnimation; + animationSet.setInterpolator(new AccelerateInterpolator()); + animationSet.addAnimation(new AlphaAnimation(0.0f, 1.0f)); + if (mOrientation == ORIENTATION_HORIZONTAL) { + animationSet.addAnimation(new TranslateAnimation(Animation.ABSOLUTE, 0.0f, + Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, + Animation.RELATIVE_TO_SELF, 0.0f)); + } else { + animationSet.addAnimation(new TranslateAnimation(Animation.RELATIVE_TO_SELF, + 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.ABSOLUTE, 0.0f, + Animation.ABSOLUTE, 0.0f)); + } + animationSet.setDuration(ANIMATION_DURATION); + } + if (mHandleInAnimation == null) { + if (mOrientation == ORIENTATION_HORIZONTAL) { + mHandleInAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0.0f, + Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, + Animation.RELATIVE_TO_SELF, 0.0f); + } else { + mHandleInAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, + 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.ABSOLUTE, 0.0f, + Animation.ABSOLUTE, 0.0f); + } + mHandleInAnimation.setDuration(ANIMATION_DURATION); + } + if (mOutAnimation == null) { + mOutAnimation = new FastAnimationSet(); + final AnimationSet animationSet = mOutAnimation; + animationSet.setInterpolator(new AccelerateInterpolator()); + animationSet.addAnimation(new AlphaAnimation(1.0f, 0.0f)); + if (mOrientation == ORIENTATION_HORIZONTAL) { + animationSet.addAnimation(new FastTranslateAnimation(Animation.ABSOLUTE, 0.0f, + Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, + Animation.RELATIVE_TO_SELF, 1.0f)); + } else { + animationSet.addAnimation(new FastTranslateAnimation(Animation.RELATIVE_TO_SELF, + 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.ABSOLUTE, 0.0f, + Animation.ABSOLUTE, 0.0f)); + } + animationSet.setDuration(ANIMATION_DURATION); + } + if (mHandleOutAnimation == null) { + if (mOrientation == ORIENTATION_HORIZONTAL) { + mHandleOutAnimation = new FastTranslateAnimation(Animation.ABSOLUTE, 0.0f, + Animation.ABSOLUTE, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, + Animation.RELATIVE_TO_SELF, 1.0f); + } else { + mHandleOutAnimation = new FastTranslateAnimation(Animation.RELATIVE_TO_SELF, + 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.ABSOLUTE, 0.0f, + Animation.ABSOLUTE, 0.0f); + } + mHandleOutAnimation.setFillAfter(true); + mHandleOutAnimation.setDuration(ANIMATION_DURATION); + } + } + + void setLauncher(Launcher launcher) { + mLauncher = launcher; + } + + void setDragController(DragLayer dragLayer) { + mDragLayer = dragLayer; + } + + void setHandle(View view) { + mHandle = view; + } + + private static class FastTranslateAnimation extends TranslateAnimation { + public FastTranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, + int fromYType, float fromYValue, int toYType, float toYValue) { + super(fromXType, fromXValue, toXType, toXValue, + fromYType, fromYValue, toYType, toYValue); + } + + @Override + public boolean willChangeTransformationMatrix() { + return true; + } + + @Override + public boolean willChangeBounds() { + return false; + } + } + + private static class FastAnimationSet extends AnimationSet { + FastAnimationSet() { + super(false); + } + + @Override + public boolean willChangeTransformationMatrix() { + return true; + } + + @Override + public boolean willChangeBounds() { + return false; + } + } +} diff --git a/src/com/android/launcher2/DragController.java b/src/com/android/launcher2/DragController.java new file mode 100644 index 0000000..29cf15a --- /dev/null +++ b/src/com/android/launcher2/DragController.java @@ -0,0 +1,79 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.view.View; + +/** + * Interface for initiating a drag within a view or across multiple views. + * + */ +public interface DragController { + + /** + * Interface to receive notifications when a drag starts or stops + */ + interface DragListener { + + /** + * A drag has begun + * + * @param v The view that is being dragged + * @param source An object representing where the drag originated + * @param info The data associated with the object that is being dragged + * @param dragAction The drag action: either {@link DragController#DRAG_ACTION_MOVE} + * or {@link DragController#DRAG_ACTION_COPY} + */ + void onDragStart(View v, DragSource source, Object info, int dragAction); + + /** + * The drag has eneded + */ + void onDragEnd(); + } + + /** + * Indicates the drag is a move. + */ + public static int DRAG_ACTION_MOVE = 0; + + /** + * Indicates the drag is a copy. + */ + public static int DRAG_ACTION_COPY = 1; + + /** + * Starts a drag + * + * @param v The view that is being dragged + * @param source An object representing where the drag originated + * @param info The data associated with the object that is being dragged + * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or + * {@link #DRAG_ACTION_COPY} + */ + void startDrag(View v, DragSource source, Object info, int dragAction); + + /** + * Sets the drag listner which will be notified when a drag starts or ends. + */ + void setDragListener(DragListener l); + + /** + * Remove a previously installed drag listener. + */ + void removeDragListener(DragListener l); +} diff --git a/src/com/android/launcher2/DragLayer.java b/src/com/android/launcher2/DragLayer.java new file mode 100644 index 0000000..070c938 --- /dev/null +++ b/src/com/android/launcher2/DragLayer.java @@ -0,0 +1,632 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Matrix; +import android.graphics.Rect; +import android.graphics.RectF; +import android.graphics.Paint; +import android.graphics.PorterDuffColorFilter; +import android.graphics.PorterDuff; +import android.os.Vibrator; +import android.os.SystemClock; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.view.View; +import android.view.ViewGroup; +import android.view.KeyEvent; +import android.view.inputmethod.InputMethodManager; +import android.widget.FrameLayout; + +/** + * A ViewGroup that coordinated dragging across its dscendants + */ +public class DragLayer extends FrameLayout implements DragController { + private static final int SCROLL_DELAY = 600; + private static final int SCROLL_ZONE = 20; + private static final int VIBRATE_DURATION = 35; + private static final int ANIMATION_SCALE_UP_DURATION = 110; + + private static final boolean PROFILE_DRAWING_DURING_DRAG = false; + + // Number of pixels to add to the dragged item for scaling + private static final float DRAG_SCALE = 24.0f; + + private boolean mDragging = false; + private boolean mShouldDrop; + private float mLastMotionX; + private float mLastMotionY; + + /** + * The bitmap that is currently being dragged + */ + private Bitmap mDragBitmap = null; + private View mOriginator; + + private int mBitmapOffsetX; + private int mBitmapOffsetY; + + /** + * X offset from where we touched on the cell to its upper-left corner + */ + private float mTouchOffsetX; + + /** + * Y offset from where we touched on the cell to its upper-left corner + */ + private float mTouchOffsetY; + + /** + * Utility rectangle + */ + private Rect mDragRect = new Rect(); + + /** + * Where the drag originated + */ + private DragSource mDragSource; + + /** + * The data associated with the object being dragged + */ + private Object mDragInfo; + + private final Rect mRect = new Rect(); + private final int[] mDropCoordinates = new int[2]; + + private final Vibrator mVibrator = new Vibrator(); + + private DragListener mListener; + + private DragScroller mDragScroller; + + private static final int SCROLL_OUTSIDE_ZONE = 0; + private static final int SCROLL_WAITING_IN_ZONE = 1; + + private static final int SCROLL_LEFT = 0; + private static final int SCROLL_RIGHT = 1; + + private int mScrollState = SCROLL_OUTSIDE_ZONE; + + private ScrollRunnable mScrollRunnable = new ScrollRunnable(); + private View mIgnoredDropTarget; + + private RectF mDragRegion; + private boolean mEnteredRegion; + private DropTarget mLastDropTarget; + + private final Paint mTrashPaint = new Paint(); + private final Paint mEstimatedPaint = new Paint(); + private Paint mDragPaint; + + /** + * If true, draw a "snag" showing where the object currently being dragged + * would end up if dropped from current location. + */ + private static final boolean DRAW_TARGET_SNAG = false; + + private Rect mEstimatedRect = new Rect(); + private float[] mDragCenter = new float[2]; + private float[] mEstimatedCenter = new float[2]; + private boolean mDrawEstimated = false; + + private int mTriggerWidth = -1; + private int mTriggerHeight = -1; + + private static final int DISTANCE_DRAW_SNAG = 20; + + private static final int ANIMATION_STATE_STARTING = 1; + private static final int ANIMATION_STATE_RUNNING = 2; + private static final int ANIMATION_STATE_DONE = 3; + + private static final int ANIMATION_TYPE_SCALE = 1; + + private float mAnimationFrom; + private float mAnimationTo; + private int mAnimationDuration; + private long mAnimationStartTime; + private int mAnimationType; + private int mAnimationState = ANIMATION_STATE_DONE; + + private InputMethodManager mInputMethodManager; + + /** + * Used to create a new DragLayer from XML. + * + * @param context The application's context. + * @param attrs The attribtues set containing the Workspace's customization values. + */ + public DragLayer(Context context, AttributeSet attrs) { + super(context, attrs); + + final int srcColor = context.getResources().getColor(R.color.delete_color_filter); + mTrashPaint.setColorFilter(new PorterDuffColorFilter(srcColor, PorterDuff.Mode.SRC_ATOP)); + + // Make estimated paint area in gray + int snagColor = context.getResources().getColor(R.color.snag_callout_color); + mEstimatedPaint.setColor(snagColor); + mEstimatedPaint.setStrokeWidth(3); + mEstimatedPaint.setAntiAlias(true); + + } + + public void startDrag(View v, DragSource source, Object dragInfo, int dragAction) { + if (PROFILE_DRAWING_DURING_DRAG) { + android.os.Debug.startMethodTracing("Launcher"); + } + + // Hide soft keyboard, if visible + if (mInputMethodManager == null) { + mInputMethodManager = (InputMethodManager) + getContext().getSystemService(Context.INPUT_METHOD_SERVICE); + } + mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0); + + if (mListener != null) { + mListener.onDragStart(v, source, dragInfo, dragAction); + } + + Rect r = mDragRect; + r.set(v.getScrollX(), v.getScrollY(), 0, 0); + + offsetDescendantRectToMyCoords(v, r); + mTouchOffsetX = mLastMotionX - r.left; + mTouchOffsetY = mLastMotionY - r.top; + + v.clearFocus(); + v.setPressed(false); + + boolean willNotCache = v.willNotCacheDrawing(); + v.setWillNotCacheDrawing(false); + + // Reset the drawing cache background color to fully transparent + // for the duration of this operation + int color = v.getDrawingCacheBackgroundColor(); + v.setDrawingCacheBackgroundColor(0); + + if (color != 0) { + v.destroyDrawingCache(); + } + v.buildDrawingCache(); + Bitmap viewBitmap = v.getDrawingCache(); + int width = viewBitmap.getWidth(); + int height = viewBitmap.getHeight(); + + mTriggerWidth = width * 2 / 3; + mTriggerHeight = height * 2 / 3; + + Matrix scale = new Matrix(); + float scaleFactor = v.getWidth(); + scaleFactor = (scaleFactor + DRAG_SCALE) /scaleFactor; + scale.setScale(scaleFactor, scaleFactor); + + mAnimationTo = 1.0f; + mAnimationFrom = 1.0f / scaleFactor; + mAnimationDuration = ANIMATION_SCALE_UP_DURATION; + mAnimationState = ANIMATION_STATE_STARTING; + mAnimationType = ANIMATION_TYPE_SCALE; + + mDragBitmap = Bitmap.createBitmap(viewBitmap, 0, 0, width, height, scale, true); + v.destroyDrawingCache(); + v.setWillNotCacheDrawing(willNotCache); + v.setDrawingCacheBackgroundColor(color); + + final Bitmap dragBitmap = mDragBitmap; + mBitmapOffsetX = (dragBitmap.getWidth() - width) / 2; + mBitmapOffsetY = (dragBitmap.getHeight() - height) / 2; + + if (dragAction == DRAG_ACTION_MOVE) { + v.setVisibility(GONE); + } + + mDragPaint = null; + mDragging = true; + mShouldDrop = true; + mOriginator = v; + mDragSource = source; + mDragInfo = dragInfo; + + mVibrator.vibrate(VIBRATE_DURATION); + + mEnteredRegion = false; + + invalidate(); + } + + @Override + public boolean dispatchKeyEvent(KeyEvent event) { + return mDragging || super.dispatchKeyEvent(event); + } + + @Override + protected void dispatchDraw(Canvas canvas) { + super.dispatchDraw(canvas); + + if (mDragging && mDragBitmap != null) { + if (mAnimationState == ANIMATION_STATE_STARTING) { + mAnimationStartTime = SystemClock.uptimeMillis(); + mAnimationState = ANIMATION_STATE_RUNNING; + } + + if (mAnimationState == ANIMATION_STATE_RUNNING) { + float normalized = (float) (SystemClock.uptimeMillis() - mAnimationStartTime) / + mAnimationDuration; + if (normalized >= 1.0f) { + mAnimationState = ANIMATION_STATE_DONE; + } + normalized = Math.min(normalized, 1.0f); + final float value = mAnimationFrom + (mAnimationTo - mAnimationFrom) * normalized; + + switch (mAnimationType) { + case ANIMATION_TYPE_SCALE: + final Bitmap dragBitmap = mDragBitmap; + canvas.save(); + canvas.translate(mScrollX + mLastMotionX - mTouchOffsetX - mBitmapOffsetX, + mScrollY + mLastMotionY - mTouchOffsetY - mBitmapOffsetY); + canvas.translate((dragBitmap.getWidth() * (1.0f - value)) / 2, + (dragBitmap.getHeight() * (1.0f - value)) / 2); + canvas.scale(value, value); + canvas.drawBitmap(dragBitmap, 0.0f, 0.0f, mDragPaint); + canvas.restore(); + break; + } + } else { + // Only draw estimate drop "snag" when requested + if (DRAW_TARGET_SNAG && mDrawEstimated) { + canvas.drawLine(mDragCenter[0], mDragCenter[1], mEstimatedCenter[0], mEstimatedCenter[1], mEstimatedPaint); + canvas.drawCircle(mEstimatedCenter[0], mEstimatedCenter[1], 8, mEstimatedPaint); + } + + // Draw actual icon being dragged + canvas.drawBitmap(mDragBitmap, + mScrollX + mLastMotionX - mTouchOffsetX - mBitmapOffsetX, + mScrollY + mLastMotionY - mTouchOffsetY - mBitmapOffsetY, mDragPaint); + } + } + } + + private void endDrag() { + if (mDragging) { + mDragging = false; + if (mDragBitmap != null) { + mDragBitmap.recycle(); + } + if (mOriginator != null) { + mOriginator.setVisibility(VISIBLE); + } + if (mListener != null) { + mListener.onDragEnd(); + } + } + } + + @Override + public boolean onInterceptTouchEvent(MotionEvent ev) { + final int action = ev.getAction(); + + final float x = ev.getX(); + final float y = ev.getY(); + + switch (action) { + case MotionEvent.ACTION_MOVE: + break; + + case MotionEvent.ACTION_DOWN: + // Remember location of down touch + mLastMotionX = x; + mLastMotionY = y; + mLastDropTarget = null; + break; + + case MotionEvent.ACTION_CANCEL: + case MotionEvent.ACTION_UP: + if (mShouldDrop && drop(x, y)) { + mShouldDrop = false; + } + endDrag(); + break; + } + + return mDragging; + } + + @Override + public boolean onTouchEvent(MotionEvent ev) { + if (!mDragging) { + return false; + } + + final int action = ev.getAction(); + final float x = ev.getX(); + final float y = ev.getY(); + + switch (action) { + case MotionEvent.ACTION_DOWN: + + // Remember where the motion event started + mLastMotionX = x; + mLastMotionY = y; + + if ((x < SCROLL_ZONE) || (x > getWidth() - SCROLL_ZONE)) { + mScrollState = SCROLL_WAITING_IN_ZONE; + postDelayed(mScrollRunnable, SCROLL_DELAY); + } else { + mScrollState = SCROLL_OUTSIDE_ZONE; + } + + break; + case MotionEvent.ACTION_MOVE: + final int scrollX = mScrollX; + final int scrollY = mScrollY; + + final float touchX = mTouchOffsetX; + final float touchY = mTouchOffsetY; + + final int offsetX = mBitmapOffsetX; + final int offsetY = mBitmapOffsetY; + + int left = (int) (scrollX + mLastMotionX - touchX - offsetX); + int top = (int) (scrollY + mLastMotionY - touchY - offsetY); + + final Bitmap dragBitmap = mDragBitmap; + final int width = dragBitmap.getWidth(); + final int height = dragBitmap.getHeight(); + + final Rect rect = mRect; + rect.set(left - 1, top - 1, left + width + 1, top + height + 1); + + mLastMotionX = x; + mLastMotionY = y; + + left = (int) (scrollX + x - touchX - offsetX); + top = (int) (scrollY + y - touchY - offsetY); + + // Invalidate current icon position + rect.union(left - 1, top - 1, left + width + 1, top + height + 1); + + mDragCenter[0] = rect.centerX(); + mDragCenter[1] = rect.centerY(); + + // Invalidate any old estimated location + if (DRAW_TARGET_SNAG && mDrawEstimated) { + rect.union(mEstimatedRect); + } + + final int[] coordinates = mDropCoordinates; + DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates); + if (dropTarget != null) { + if (mLastDropTarget == dropTarget) { + dropTarget.onDragOver(mDragSource, coordinates[0], coordinates[1], + (int) mTouchOffsetX, (int) mTouchOffsetY, mDragInfo); + } else { + if (mLastDropTarget != null) { + mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1], + (int) mTouchOffsetX, (int) mTouchOffsetY, mDragInfo); + } + dropTarget.onDragEnter(mDragSource, coordinates[0], coordinates[1], + (int) mTouchOffsetX, (int) mTouchOffsetY, mDragInfo); + } + } else { + if (mLastDropTarget != null) { + mLastDropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1], + (int) mTouchOffsetX, (int) mTouchOffsetY, mDragInfo); + } + } + + // Render estimated drop "snag" only outside of width + mDrawEstimated = false; + if (DRAW_TARGET_SNAG && dropTarget != null) { + Rect foundEstimate = dropTarget.estimateDropLocation(mDragSource, + (int) (scrollX + mLastMotionX), (int) (scrollY + mLastMotionY), + (int) mTouchOffsetX, (int) mTouchOffsetY, mDragInfo, mEstimatedRect); + + if (foundEstimate != null) { + mEstimatedCenter[0] = foundEstimate.centerX(); + mEstimatedCenter[1] = foundEstimate.centerY(); + + int deltaX = (int) Math.abs(mEstimatedCenter[0] - mDragCenter[0]); + int deltaY = (int) Math.abs(mEstimatedCenter[1] - mDragCenter[1]); + + if (deltaX > mTriggerWidth || deltaY > mTriggerHeight) { + mDrawEstimated = true; + } + } + } + + // Include new estimated area in invalidated rectangle + if (DRAW_TARGET_SNAG && mDrawEstimated) { + rect.union(mEstimatedRect); + } + invalidate(rect); + + mLastDropTarget = dropTarget; + + boolean inDragRegion = false; + if (mDragRegion != null) { + final RectF region = mDragRegion; + final boolean inRegion = region.contains(ev.getRawX(), ev.getRawY()); + if (!mEnteredRegion && inRegion) { + mDragPaint = mTrashPaint; + mEnteredRegion = true; + inDragRegion = true; + } else if (mEnteredRegion && !inRegion) { + mDragPaint = null; + mEnteredRegion = false; + } + } + + if (!inDragRegion && x < SCROLL_ZONE) { + if (mScrollState == SCROLL_OUTSIDE_ZONE) { + mScrollState = SCROLL_WAITING_IN_ZONE; + mScrollRunnable.setDirection(SCROLL_LEFT); + postDelayed(mScrollRunnable, SCROLL_DELAY); + } + } else if (!inDragRegion && x > getWidth() - SCROLL_ZONE) { + if (mScrollState == SCROLL_OUTSIDE_ZONE) { + mScrollState = SCROLL_WAITING_IN_ZONE; + mScrollRunnable.setDirection(SCROLL_RIGHT); + postDelayed(mScrollRunnable, SCROLL_DELAY); + } + } else { + if (mScrollState == SCROLL_WAITING_IN_ZONE) { + mScrollState = SCROLL_OUTSIDE_ZONE; + mScrollRunnable.setDirection(SCROLL_RIGHT); + removeCallbacks(mScrollRunnable); + } + } + + break; + case MotionEvent.ACTION_UP: + removeCallbacks(mScrollRunnable); + if (mShouldDrop) { + drop(x, y); + mShouldDrop = false; + } + endDrag(); + + break; + case MotionEvent.ACTION_CANCEL: + endDrag(); + } + + return true; + } + + private boolean drop(float x, float y) { + invalidate(); + + final int[] coordinates = mDropCoordinates; + DropTarget dropTarget = findDropTarget((int) x, (int) y, coordinates); + + if (dropTarget != null) { + dropTarget.onDragExit(mDragSource, coordinates[0], coordinates[1], + (int) mTouchOffsetX, (int) mTouchOffsetY, mDragInfo); + if (dropTarget.acceptDrop(mDragSource, coordinates[0], coordinates[1], + (int) mTouchOffsetX, (int) mTouchOffsetY, mDragInfo)) { + dropTarget.onDrop(mDragSource, coordinates[0], coordinates[1], + (int) mTouchOffsetX, (int) mTouchOffsetY, mDragInfo); + mDragSource.onDropCompleted((View) dropTarget, true); + return true; + } else { + mDragSource.onDropCompleted((View) dropTarget, false); + return true; + } + } + return false; + } + + DropTarget findDropTarget(int x, int y, int[] dropCoordinates) { + return findDropTarget(this, x, y, dropCoordinates); + } + + private DropTarget findDropTarget(ViewGroup container, int x, int y, int[] dropCoordinates) { + final Rect r = mDragRect; + final int count = container.getChildCount(); + final int scrolledX = x + container.getScrollX(); + final int scrolledY = y + container.getScrollY(); + final View ignoredDropTarget = mIgnoredDropTarget; + + for (int i = count - 1; i >= 0; i--) { + final View child = container.getChildAt(i); + if (child.getVisibility() == VISIBLE && child != ignoredDropTarget) { + child.getHitRect(r); + if (r.contains(scrolledX, scrolledY)) { + DropTarget target = null; + if (child instanceof ViewGroup) { + x = scrolledX - child.getLeft(); + y = scrolledY - child.getTop(); + target = findDropTarget((ViewGroup) child, x, y, dropCoordinates); + } + if (target == null) { + if (child instanceof DropTarget) { + // Only consider this child if they will accept + DropTarget childTarget = (DropTarget) child; + if (childTarget.acceptDrop(mDragSource, x, y, 0, 0, mDragInfo)) { + dropCoordinates[0] = x; + dropCoordinates[1] = y; + return (DropTarget) child; + } else { + return null; + } + } + } else { + return target; + } + } + } + } + + return null; + } + + public void setDragScoller(DragScroller scroller) { + mDragScroller = scroller; + } + + public void setDragListener(DragListener l) { + mListener = l; + } + + public void removeDragListener(DragListener l) { + mListener = null; + } + + /** + * Specifies the view that must be ignored when looking for a drop target. + * + * @param view The view that will not be taken into account while looking + * for a drop target. + */ + void setIgnoredDropTarget(View view) { + mIgnoredDropTarget = view; + } + + /** + * Specifies the delete region. + * + * @param region The rectangle in screen coordinates of the delete region. + */ + void setDeleteRegion(RectF region) { + mDragRegion = region; + } + + private class ScrollRunnable implements Runnable { + private int mDirection; + + ScrollRunnable() { + } + + public void run() { + if (mDragScroller != null) { + mDrawEstimated = false; + if (mDirection == SCROLL_LEFT) { + mDragScroller.scrollLeft(); + } else { + mDragScroller.scrollRight(); + } + mScrollState = SCROLL_OUTSIDE_ZONE; + } + } + + void setDirection(int direction) { + mDirection = direction; + } + } +} diff --git a/src/com/android/launcher2/DragScroller.java b/src/com/android/launcher2/DragScroller.java new file mode 100644 index 0000000..2c18a79 --- /dev/null +++ b/src/com/android/launcher2/DragScroller.java @@ -0,0 +1,26 @@ +/* + * 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. + */ + +package com.android.launcher; + +/** + * Handles scrolling while dragging + * + */ +public interface DragScroller { + void scrollLeft(); + void scrollRight(); +} diff --git a/src/com/android/launcher2/DragSource.java b/src/com/android/launcher2/DragSource.java new file mode 100644 index 0000000..0ac25bb --- /dev/null +++ b/src/com/android/launcher2/DragSource.java @@ -0,0 +1,28 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.view.View; + +/** + * Interface defining an object that can originate a drag. + * + */ +public interface DragSource { + void setDragger(DragController dragger); + void onDropCompleted(View target, boolean success); +} diff --git a/src/com/android/launcher2/DropTarget.java b/src/com/android/launcher2/DropTarget.java new file mode 100644 index 0000000..4835323 --- /dev/null +++ b/src/com/android/launcher2/DropTarget.java @@ -0,0 +1,81 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.graphics.Rect; + +/** + * Interface defining an object that can receive a drag. + * + */ +public interface DropTarget { + + /** + * Handle an object being dropped on the DropTarget + * + * @param source DragSource where the drag started + * @param x X coordinate of the drop location + * @param y Y coordinate of the drop location + * @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 dragInfo Data associated with the object being dragged + * + */ + void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo); + + void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo); + + void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo); + + void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo); + + /** + * Check if a drop action can occur at, or near, the requested location. + * This may be called repeatedly during a drag, so any calls should return + * quickly. + * + * @param source DragSource where the drag started + * @param x X coordinate of the drop location + * @param y Y coordinate of the drop location + * @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 dragInfo Data associated with the object being dragged + * @return True if the drop will be accepted, false otherwise. + */ + boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo); + + /** + * Estimate the surface area where this object would land if dropped at the + * given location. + * + * @param source DragSource where the drag started + * @param x X coordinate of the drop location + * @param y Y coordinate of the drop location + * @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 dragInfo Data associated with the object being dragged + * @param recycle {@link Rect} object to be possibly recycled. + * @return Estimated area that would be occupied if object was dropped at + * the given location. Should return null if no estimate is found, + * or if this target doesn't provide estimations. + */ + Rect estimateDropLocation(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo, Rect recycle); +} diff --git a/src/com/android/launcher2/FastBitmapDrawable.java b/src/com/android/launcher2/FastBitmapDrawable.java new file mode 100644 index 0000000..170f1ad --- /dev/null +++ b/src/com/android/launcher2/FastBitmapDrawable.java @@ -0,0 +1,73 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.graphics.drawable.Drawable; +import android.graphics.PixelFormat; +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.ColorFilter; + +class FastBitmapDrawable extends Drawable { + private Bitmap mBitmap; + + FastBitmapDrawable(Bitmap b) { + mBitmap = b; + } + + @Override + public void draw(Canvas canvas) { + canvas.drawBitmap(mBitmap, 0.0f, 0.0f, null); + } + + @Override + public int getOpacity() { + return PixelFormat.TRANSLUCENT; + } + + @Override + public void setAlpha(int alpha) { + } + + @Override + public void setColorFilter(ColorFilter cf) { + } + + @Override + public int getIntrinsicWidth() { + return mBitmap.getWidth(); + } + + @Override + public int getIntrinsicHeight() { + return mBitmap.getHeight(); + } + + @Override + public int getMinimumWidth() { + return mBitmap.getWidth(); + } + + @Override + public int getMinimumHeight() { + return mBitmap.getHeight(); + } + + public Bitmap getBitmap() { + return mBitmap; + } +} diff --git a/src/com/android/launcher2/Folder.java b/src/com/android/launcher2/Folder.java new file mode 100644 index 0000000..fb4e8d6 --- /dev/null +++ b/src/com/android/launcher2/Folder.java @@ -0,0 +1,159 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.AdapterView; +import android.widget.Button; +import android.widget.LinearLayout; +import android.widget.AbsListView; +import android.widget.BaseAdapter; +import android.widget.AdapterView.OnItemClickListener; +import android.widget.AdapterView.OnItemLongClickListener; + +/** + * Represents a set of icons chosen by the user or generated by the system. + */ +public class Folder extends LinearLayout implements DragSource, OnItemLongClickListener, + OnItemClickListener, OnClickListener, View.OnLongClickListener { + + protected AbsListView mContent; + protected DragController mDragger; + + protected Launcher mLauncher; + + protected Button mCloseButton; + + protected FolderInfo mInfo; + + /** + * Which item is being dragged + */ + protected ApplicationInfo mDragItem; + private boolean mCloneInfo; + + /** + * Used to inflate the Workspace from XML. + * + * @param context The application's context. + * @param attrs The attribtues set containing the Workspace's customization values. + */ + public Folder(Context context, AttributeSet attrs) { + super(context, attrs); + setAlwaysDrawnWithCacheEnabled(false); + } + + @Override + protected void onFinishInflate() { + super.onFinishInflate(); + + mContent = (AbsListView) findViewById(R.id.content); + mContent.setOnItemClickListener(this); + mContent.setOnItemLongClickListener(this); + + mCloseButton = (Button) findViewById(R.id.close); + mCloseButton.setOnClickListener(this); + mCloseButton.setOnLongClickListener(this); + } + + public void onItemClick(AdapterView parent, View v, int position, long id) { + ApplicationInfo app = (ApplicationInfo) parent.getItemAtPosition(position); + mLauncher.startActivitySafely(app.intent); + } + + public void onClick(View v) { + mLauncher.closeFolder(this); + } + + public boolean onLongClick(View v) { + mLauncher.closeFolder(this); + mLauncher.showRenameDialog(mInfo); + return true; + } + + public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { + if (!view.isInTouchMode()) { + return false; + } + + ApplicationInfo app = (ApplicationInfo) parent.getItemAtPosition(position); + if (mCloneInfo) { + app = new ApplicationInfo(app); + } + + mDragger.startDrag(view, this, app, DragController.DRAG_ACTION_COPY); + mLauncher.closeFolder(this); + mDragItem = app; + + return true; + } + + void setCloneInfo(boolean cloneInfo) { + mCloneInfo = cloneInfo; + } + + public void setDragger(DragController dragger) { + mDragger = dragger; + } + + public void onDropCompleted(View target, boolean success) { + } + + /** + * Sets the adapter used to populate the content area. The adapter must only + * contains ApplicationInfo items. + * + * @param adapter The list of applications to display in the folder. + */ + void setContentAdapter(BaseAdapter adapter) { + mContent.setAdapter(adapter); + } + + void notifyDataSetChanged() { + ((BaseAdapter) mContent.getAdapter()).notifyDataSetChanged(); + } + + void setLauncher(Launcher launcher) { + mLauncher = launcher; + } + + /** + * @return the FolderInfo object associated with this folder + */ + FolderInfo getInfo() { + return mInfo; + } + + // When the folder opens, we need to refresh the GridView's selection by + // forcing a layout + void onOpen() { + mContent.requestLayout(); + } + + void onClose() { + final Workspace workspace = mLauncher.getWorkspace(); + workspace.getChildAt(workspace.getCurrentScreen()).requestFocus(); + } + + void bind(FolderInfo info) { + mInfo = info; + mCloseButton.setText(info.title); + } +} diff --git a/src/com/android/launcher2/FolderIcon.java b/src/com/android/launcher2/FolderIcon.java new file mode 100644 index 0000000..a56101d --- /dev/null +++ b/src/com/android/launcher2/FolderIcon.java @@ -0,0 +1,97 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.content.Context; +import android.content.res.Resources; +import android.graphics.Rect; +import android.graphics.drawable.Drawable; +import android.util.AttributeSet; +import android.view.LayoutInflater; +import android.view.ViewGroup; + +/** + * An icon that can appear on in the workspace representing an {@link UserFolder}. + */ +public class FolderIcon extends BubbleTextView implements DropTarget { + private UserFolderInfo mInfo; + private Launcher mLauncher; + private Drawable mCloseIcon; + private Drawable mOpenIcon; + + public FolderIcon(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public FolderIcon(Context context) { + super(context); + } + + static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group, + UserFolderInfo folderInfo) { + + FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false); + + final Resources resources = launcher.getResources(); + Drawable d = resources.getDrawable(R.drawable.ic_launcher_folder); + d = Utilities.createIconThumbnail(d, launcher); + icon.mCloseIcon = d; + icon.mOpenIcon = resources.getDrawable(R.drawable.ic_launcher_folder_open); + icon.setCompoundDrawablesWithIntrinsicBounds(null, d, null, null); + icon.setText(folderInfo.title); + icon.setTag(folderInfo); + icon.setOnClickListener(launcher); + icon.mInfo = folderInfo; + icon.mLauncher = launcher; + + return icon; + } + + public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset, + Object dragInfo) { + final ItemInfo item = (ItemInfo) dragInfo; + final int itemType = item.itemType; + return (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION || + itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) + && item.container != mInfo.id; + } + + public Rect estimateDropLocation(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo, Rect recycle) { + return null; + } + + public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo) { + final ApplicationInfo item = (ApplicationInfo) dragInfo; + // TODO: update open folder that is looking at this data + mInfo.add(item); + LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, 0, 0); + } + + public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset, + Object dragInfo) { + setCompoundDrawablesWithIntrinsicBounds(null, mOpenIcon, null, null); + } + + public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset, + Object dragInfo) { + } + + public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset, + Object dragInfo) { + setCompoundDrawablesWithIntrinsicBounds(null, mCloseIcon, null, null); + } +} diff --git a/src/com/android/launcher2/FolderInfo.java b/src/com/android/launcher2/FolderInfo.java new file mode 100644 index 0000000..a58675b --- /dev/null +++ b/src/com/android/launcher2/FolderInfo.java @@ -0,0 +1,34 @@ +/* + * 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. + */ + +package com.android.launcher; + + +/** + * Represents a folder containing shortcuts or apps. + */ +class FolderInfo extends ItemInfo { + + /** + * Whether this folder has been opened + */ + boolean opened; + + /** + * The folder name. + */ + CharSequence title; +} diff --git a/src/com/android/launcher2/HandleView.java b/src/com/android/launcher2/HandleView.java new file mode 100644 index 0000000..9afe41c --- /dev/null +++ b/src/com/android/launcher2/HandleView.java @@ -0,0 +1,91 @@ +/* + * 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. + */ + + +package com.android.launcher; + +import android.widget.ImageView; +import android.content.Context; +import android.content.res.TypedArray; +import android.util.AttributeSet; +import android.view.View; +import android.view.KeyEvent; + +public class HandleView extends ImageView { + private static final int ORIENTATION_HORIZONTAL = 1; + + private Launcher mLauncher; + private int mOrientation = ORIENTATION_HORIZONTAL; + + public HandleView(Context context) { + super(context); + } + + public HandleView(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public HandleView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HandleView, defStyle, 0); + mOrientation = a.getInt(R.styleable.HandleView_direction, ORIENTATION_HORIZONTAL); + a.recycle(); + } + + @Override + public View focusSearch(int direction) { + View newFocus = super.focusSearch(direction); + if (newFocus == null && mLauncher.isDrawerDown()) { + final Workspace workspace = mLauncher.getWorkspace(); + workspace.dispatchUnhandledMove(null, direction); + return (mOrientation == ORIENTATION_HORIZONTAL && direction == FOCUS_DOWN) ? + this : workspace; + } + return newFocus; + } + + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + final boolean handled = super.onKeyDown(keyCode, event); + + if (!handled && !mLauncher.isDrawerDown() && !isDirectionKey(keyCode)) { + return mLauncher.getApplicationsGrid().onKeyDown(keyCode, event); + } + + return handled; + } + + @Override + public boolean onKeyUp(int keyCode, KeyEvent event) { + final boolean handled = super.onKeyUp(keyCode, event); + + if (!handled && !mLauncher.isDrawerDown() && !isDirectionKey(keyCode)) { + return mLauncher.getApplicationsGrid().onKeyUp(keyCode, event); + } + + return handled; + } + + private static boolean isDirectionKey(int keyCode) { + return keyCode == KeyEvent.KEYCODE_DPAD_DOWN || keyCode == KeyEvent.KEYCODE_DPAD_LEFT || + keyCode == KeyEvent.KEYCODE_DPAD_RIGHT || keyCode == KeyEvent.KEYCODE_DPAD_UP; + } + + void setLauncher(Launcher launcher) { + mLauncher = launcher; + } +} diff --git a/src/com/android/launcher2/InstallShortcutReceiver.java b/src/com/android/launcher2/InstallShortcutReceiver.java new file mode 100644 index 0000000..7c727a8 --- /dev/null +++ b/src/com/android/launcher2/InstallShortcutReceiver.java @@ -0,0 +1,122 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.ContentResolver; +import android.database.Cursor; +import android.widget.Toast; + +public class InstallShortcutReceiver extends BroadcastReceiver { + private static final String ACTION_INSTALL_SHORTCUT = + "com.android.launcher.action.INSTALL_SHORTCUT"; + + private final int[] mCoordinates = new int[2]; + + public void onReceive(Context context, Intent data) { + if (!ACTION_INSTALL_SHORTCUT.equals(data.getAction())) { + return; + } + + int screen = Launcher.getScreen(); + + if (!installShortcut(context, data, screen)) { + // The target screen is full, let's try the other screens + for (int i = 0; i < Launcher.SCREEN_COUNT; i++) { + if (i != screen && installShortcut(context, data, i)) break; + } + } + } + + private boolean installShortcut(Context context, Intent data, int screen) { + String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); + + if (findEmptyCell(context, mCoordinates, screen)) { + CellLayout.CellInfo cell = new CellLayout.CellInfo(); + cell.cellX = mCoordinates[0]; + cell.cellY = mCoordinates[1]; + cell.screen = screen; + + Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT); + + if (intent.getAction() == null) { + intent.setAction(Intent.ACTION_VIEW); + } + + // By default, we allow for duplicate entries (located in + // different places) + boolean duplicate = data.getBooleanExtra(Launcher.EXTRA_SHORTCUT_DUPLICATE, true); + if (duplicate || !LauncherModel.shortcutExists(context, name, intent)) { + Launcher.addShortcut(context, data, cell, true); + Toast.makeText(context, context.getString(R.string.shortcut_installed, name), + Toast.LENGTH_SHORT).show(); + } else { + Toast.makeText(context, context.getString(R.string.shortcut_duplicate, name), + Toast.LENGTH_SHORT).show(); + } + + return true; + } else { + Toast.makeText(context, context.getString(R.string.out_of_space), + Toast.LENGTH_SHORT).show(); + } + + return false; + } + + private static boolean findEmptyCell(Context context, int[] xy, int screen) { + final int xCount = Launcher.NUMBER_CELLS_X; + final int yCount = Launcher.NUMBER_CELLS_Y; + + boolean[][] occupied = new boolean[xCount][yCount]; + + final ContentResolver cr = context.getContentResolver(); + Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, + new String[] { LauncherSettings.Favorites.CELLX, LauncherSettings.Favorites.CELLY, + LauncherSettings.Favorites.SPANX, LauncherSettings.Favorites.SPANY }, + LauncherSettings.Favorites.SCREEN + "=?", + new String[] { String.valueOf(screen) }, null); + + final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX); + final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY); + final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX); + final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY); + + try { + while (c.moveToNext()) { + int cellX = c.getInt(cellXIndex); + int cellY = c.getInt(cellYIndex); + int spanX = c.getInt(spanXIndex); + int spanY = c.getInt(spanYIndex); + + for (int x = cellX; x < cellX + spanX && x < xCount; x++) { + for (int y = cellY; y < cellY + spanY && y < yCount; y++) { + occupied[x][y] = true; + } + } + } + } catch (Exception e) { + return false; + } finally { + c.close(); + } + + return CellLayout.findVacantCell(xy, 1, 1, xCount, yCount, occupied); + } +} diff --git a/src/com/android/launcher2/ItemInfo.java b/src/com/android/launcher2/ItemInfo.java new file mode 100644 index 0000000..71cee18 --- /dev/null +++ b/src/com/android/launcher2/ItemInfo.java @@ -0,0 +1,133 @@ +/* + * 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. + */ + +package com.android.launcher; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import android.content.ContentValues; +import android.graphics.Bitmap; +import android.util.Log; + +/** + * Represents an item in the launcher. + */ +class ItemInfo { + + static final int NO_ID = -1; + + /** + * The id in the settings database for this item + */ + long id = NO_ID; + + /** + * One of {@link LauncherSettings.Favorites#ITEM_TYPE_APPLICATION}, + * {@link LauncherSettings.Favorites#ITEM_TYPE_SHORTCUT}, + * {@link LauncherSettings.Favorites#ITEM_TYPE_USER_FOLDER}, or + * {@link LauncherSettings.Favorites#ITEM_TYPE_APPWIDGET}. + */ + int itemType; + + /** + * The id of the container that holds this item. For the desktop, this will be + * {@link LauncherSettings.Favorites#CONTAINER_DESKTOP}. For the all applications folder it + * will be {@link #NO_ID} (since it is not stored in the settings DB). For user folders + * it will be the id of the folder. + */ + long container = NO_ID; + + /** + * Iindicates the screen in which the shortcut appears. + */ + int screen = -1; + + /** + * Indicates the X position of the associated cell. + */ + int cellX = -1; + + /** + * Indicates the Y position of the associated cell. + */ + int cellY = -1; + + /** + * Indicates the X cell span. + */ + int spanX = 1; + + /** + * Indicates the Y cell span. + */ + int spanY = 1; + + /** + * Indicates whether the item is a gesture. + */ + boolean isGesture = false; + + ItemInfo() { + } + + ItemInfo(ItemInfo info) { + id = info.id; + cellX = info.cellX; + cellY = info.cellY; + spanX = info.spanX; + spanY = info.spanY; + screen = info.screen; + itemType = info.itemType; + container = info.container; + } + + /** + * Write the fields of this item to the DB + * + * @param values + */ + void onAddToDatabase(ContentValues values) { + values.put(LauncherSettings.BaseLauncherColumns.ITEM_TYPE, itemType); + if (!isGesture) { + values.put(LauncherSettings.Favorites.CONTAINER, container); + values.put(LauncherSettings.Favorites.SCREEN, screen); + values.put(LauncherSettings.Favorites.CELLX, cellX); + values.put(LauncherSettings.Favorites.CELLY, cellY); + values.put(LauncherSettings.Favorites.SPANX, spanX); + values.put(LauncherSettings.Favorites.SPANY, spanY); + } + } + + static void writeBitmap(ContentValues values, Bitmap bitmap) { + if (bitmap != null) { + // Try go guesstimate how much space the icon will take when serialized + // to avoid unnecessary allocations/copies during the write. + int size = bitmap.getWidth() * bitmap.getHeight() * 4; + ByteArrayOutputStream out = new ByteArrayOutputStream(size); + try { + bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); + out.flush(); + out.close(); + + values.put(LauncherSettings.Favorites.ICON, out.toByteArray()); + } catch (IOException e) { + Log.w("Favorite", "Could not write icon"); + } + } + } + +} diff --git a/src/com/android/launcher2/Launcher.java b/src/com/android/launcher2/Launcher.java new file mode 100644 index 0000000..f1af0b1 --- /dev/null +++ b/src/com/android/launcher2/Launcher.java @@ -0,0 +1,2247 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Application; +import android.app.Dialog; +import android.app.ISearchManager; +import android.app.IWallpaperService; +import android.app.SearchManager; +import android.app.StatusBarManager; +import android.content.ActivityNotFoundException; +import android.content.BroadcastReceiver; +import android.content.ComponentName; +import android.content.ContentResolver; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.content.IntentFilter; +import android.content.Intent.ShortcutIconResource; +import android.content.pm.ActivityInfo; +import android.content.pm.PackageManager; +import android.content.pm.PackageManager.NameNotFoundException; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.database.ContentObserver; +import android.graphics.Bitmap; +import android.graphics.Rect; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; +import android.graphics.drawable.TransitionDrawable; +import android.os.Bundle; +import android.os.Handler; +import android.os.IBinder; +import android.os.Looper; +import android.os.Message; +import android.os.MessageQueue; +import android.os.Parcelable; +import android.os.RemoteException; +import android.os.ServiceManager; +import android.provider.LiveFolders; +import android.text.Selection; +import android.text.SpannableStringBuilder; +import android.text.TextUtils; +import android.text.method.TextKeyListener; +import static android.util.Log.*; +import android.view.Display; +import android.view.KeyEvent; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.view.View.OnLongClickListener; +import android.view.inputmethod.InputMethodManager; +import android.widget.EditText; +import android.widget.GridView; +import android.widget.SlidingDrawer; +import android.widget.TextView; +import android.widget.Toast; +import android.appwidget.AppWidgetManager; +import android.appwidget.AppWidgetProviderInfo; + +import java.lang.ref.WeakReference; +import java.util.ArrayList; +import java.util.LinkedList; +import java.io.DataOutputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.DataInputStream; + +/** + * Default launcher application. + */ +public final class Launcher extends Activity implements View.OnClickListener, OnLongClickListener { + static final String LOG_TAG = "Launcher"; + static final boolean LOGD = false; + + private static final boolean PROFILE_STARTUP = false; + private static final boolean PROFILE_DRAWER = false; + private static final boolean PROFILE_ROTATE = false; + private static final boolean DEBUG_USER_INTERFACE = false; + + private static final int WALLPAPER_SCREENS_SPAN = 2; + + private static final int MENU_GROUP_ADD = 1; + private static final int MENU_ADD = Menu.FIRST + 1; + private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1; + private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1; + private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1; + private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1; + + private static final int REQUEST_CREATE_SHORTCUT = 1; + private static final int REQUEST_CREATE_LIVE_FOLDER = 4; + private static final int REQUEST_CREATE_APPWIDGET = 5; + private static final int REQUEST_PICK_APPLICATION = 6; + private static final int REQUEST_PICK_SHORTCUT = 7; + private static final int REQUEST_PICK_LIVE_FOLDER = 8; + private static final int REQUEST_PICK_APPWIDGET = 9; + + static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate"; + + static final String EXTRA_CUSTOM_WIDGET = "custom_widget"; + static final String SEARCH_WIDGET = "search_widget"; + + static final int SCREEN_COUNT = 3; + static final int DEFAULT_SCREN = 1; + static final int NUMBER_CELLS_X = 4; + static final int NUMBER_CELLS_Y = 4; + + private static final int DIALOG_CREATE_SHORTCUT = 1; + static final int DIALOG_RENAME_FOLDER = 2; + + private static final String PREFERENCES = "launcher.preferences"; + + // Type: int + private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen"; + // Type: boolean + private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder"; + // Type: long + private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder"; + // Type: int + private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen"; + // Type: int + private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX"; + // Type: int + private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY"; + // Type: int + private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX"; + // Type: int + private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY"; + // Type: int + private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX"; + // Type: int + private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY"; + // Type: int[] + private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells"; + // Type: boolean + private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder"; + // Type: long + private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id"; + + private static final LauncherModel sModel = new LauncherModel(); + + private static Bitmap sWallpaper; + + private static final Object sLock = new Object(); + private static int sScreen = DEFAULT_SCREN; + + private static WallpaperIntentReceiver sWallpaperReceiver; + + private final BroadcastReceiver mApplicationsReceiver = new ApplicationsIntentReceiver(); + private final ContentObserver mObserver = new FavoritesChangeObserver(); + + private LayoutInflater mInflater; + + private DragLayer mDragLayer; + private Workspace mWorkspace; + + private AppWidgetManager mAppWidgetManager; + private LauncherAppWidgetHost mAppWidgetHost; + + static final int APPWIDGET_HOST_ID = 1024; + + private CellLayout.CellInfo mAddItemCellInfo; + private CellLayout.CellInfo mMenuAddInfo; + private final int[] mCellCoordinates = new int[2]; + private FolderInfo mFolderInfo; + + private SlidingDrawer mDrawer; + private TransitionDrawable mHandleIcon; + private HandleView mHandleView; + private AllAppsGridView mAllAppsGrid; + + private boolean mDesktopLocked = true; + private Bundle mSavedState; + + private SpannableStringBuilder mDefaultKeySsb = null; + + private boolean mDestroyed; + + private boolean mIsNewIntent; + + private boolean mRestoring; + private boolean mWaitingForResult; + private boolean mLocaleChanged; + + private boolean mHomeDown; + private boolean mBackDown; + + private Bundle mSavedInstanceState; + + private DesktopBinder mBinder; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + mInflater = getLayoutInflater(); + + mAppWidgetManager = AppWidgetManager.getInstance(this); + + mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID); + mAppWidgetHost.startListening(); + + if (PROFILE_STARTUP) { + android.os.Debug.startMethodTracing("/sdcard/launcher"); + } + + checkForLocaleChange(); + setWallpaperDimension(); + + setContentView(R.layout.launcher); + setupViews(); + + registerIntentReceivers(); + registerContentObservers(); + + mSavedState = savedInstanceState; + restoreState(mSavedState); + + if (PROFILE_STARTUP) { + android.os.Debug.stopMethodTracing(); + } + + if (!mRestoring) { + startLoaders(); + } + + // For handling default keys + mDefaultKeySsb = new SpannableStringBuilder(); + Selection.setSelection(mDefaultKeySsb, 0); + } + + private void checkForLocaleChange() { + final LocaleConfiguration localeConfiguration = new LocaleConfiguration(); + readConfiguration(this, localeConfiguration); + + final Configuration configuration = getResources().getConfiguration(); + + final String previousLocale = localeConfiguration.locale; + final String locale = configuration.locale.toString(); + + final int previousMcc = localeConfiguration.mcc; + final int mcc = configuration.mcc; + + final int previousMnc = localeConfiguration.mnc; + final int mnc = configuration.mnc; + + mLocaleChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc; + + if (mLocaleChanged) { + localeConfiguration.locale = locale; + localeConfiguration.mcc = mcc; + localeConfiguration.mnc = mnc; + + writeConfiguration(this, localeConfiguration); + } + } + + private static class LocaleConfiguration { + public String locale; + public int mcc = -1; + public int mnc = -1; + } + + private static void readConfiguration(Context context, LocaleConfiguration configuration) { + DataInputStream in = null; + try { + in = new DataInputStream(context.openFileInput(PREFERENCES)); + configuration.locale = in.readUTF(); + configuration.mcc = in.readInt(); + configuration.mnc = in.readInt(); + } catch (FileNotFoundException e) { + // Ignore + } catch (IOException e) { + // Ignore + } finally { + if (in != null) { + try { + in.close(); + } catch (IOException e) { + // Ignore + } + } + } + } + + private static void writeConfiguration(Context context, LocaleConfiguration configuration) { + DataOutputStream out = null; + try { + out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE)); + out.writeUTF(configuration.locale); + out.writeInt(configuration.mcc); + out.writeInt(configuration.mnc); + out.flush(); + } catch (FileNotFoundException e) { + // Ignore + } catch (IOException e) { + //noinspection ResultOfMethodCallIgnored + context.getFileStreamPath(PREFERENCES).delete(); + } finally { + if (out != null) { + try { + out.close(); + } catch (IOException e) { + // Ignore + } + } + } + } + + static int getScreen() { + synchronized (sLock) { + return sScreen; + } + } + + static void setScreen(int screen) { + synchronized (sLock) { + sScreen = screen; + } + } + + private void startLoaders() { + boolean loadApplications = sModel.loadApplications(true, this, mLocaleChanged); + sModel.loadUserItems(!mLocaleChanged, this, mLocaleChanged, loadApplications); + + mRestoring = false; + } + + private void setWallpaperDimension() { + IBinder binder = ServiceManager.getService(WALLPAPER_SERVICE); + IWallpaperService wallpaperService = IWallpaperService.Stub.asInterface(binder); + + Display display = getWindowManager().getDefaultDisplay(); + boolean isPortrait = display.getWidth() < display.getHeight(); + + final int width = isPortrait ? display.getWidth() : display.getHeight(); + final int height = isPortrait ? display.getHeight() : display.getWidth(); + try { + wallpaperService.setDimensionHints(width * WALLPAPER_SCREENS_SPAN, height); + } catch (RemoteException e) { + // System is dead! + } + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + mWaitingForResult = false; + + // The pattern used here is that a user PICKs a specific application, + // which, depending on the target, might need to CREATE the actual target. + + // For example, the user would PICK_SHORTCUT for "Music playlist", and we + // launch over to the Music app to actually CREATE_SHORTCUT. + + if (resultCode == RESULT_OK && mAddItemCellInfo != null) { + switch (requestCode) { + case REQUEST_PICK_APPLICATION: + completeAddApplication(this, data, mAddItemCellInfo, !mDesktopLocked); + break; + case REQUEST_PICK_SHORTCUT: + processShortcut(data, REQUEST_PICK_APPLICATION, REQUEST_CREATE_SHORTCUT); + break; + case REQUEST_CREATE_SHORTCUT: + completeAddShortcut(data, mAddItemCellInfo, !mDesktopLocked); + break; + case REQUEST_PICK_LIVE_FOLDER: + addLiveFolder(data); + break; + case REQUEST_CREATE_LIVE_FOLDER: + completeAddLiveFolder(data, mAddItemCellInfo, !mDesktopLocked); + break; + case REQUEST_PICK_APPWIDGET: + addAppWidget(data); + break; + case REQUEST_CREATE_APPWIDGET: + completeAddAppWidget(data, mAddItemCellInfo, !mDesktopLocked); + break; + } + } else if (requestCode == REQUEST_PICK_APPWIDGET && + resultCode == RESULT_CANCELED && data != null) { + // Clean up the appWidgetId if we canceled + int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1); + if (appWidgetId != -1) { + mAppWidgetHost.deleteAppWidgetId(appWidgetId); + } + } + } + + @Override + protected void onResume() { + super.onResume(); + + if (mRestoring) { + startLoaders(); + } + + // If this was a new intent (i.e., the mIsNewIntent flag got set to true by + // onNewIntent), then close the search dialog if needed, because it probably + // came from the user pressing 'home' (rather than, for example, pressing 'back'). + if (mIsNewIntent) { + // Post to a handler so that this happens after the search dialog tries to open + // itself again. + mWorkspace.post(new Runnable() { + public void run() { + ISearchManager searchManagerService = ISearchManager.Stub.asInterface( + ServiceManager.getService(Context.SEARCH_SERVICE)); + try { + searchManagerService.stopSearch(); + } catch (RemoteException e) { + e(LOG_TAG, "error stopping search", e); + } + } + }); + } + + mIsNewIntent = false; + } + + @Override + protected void onPause() { + super.onPause(); + closeDrawer(false); + } + + @Override + public Object onRetainNonConfigurationInstance() { + // Flag any binder to stop early before switching + if (mBinder != null) { + mBinder.mTerminate = true; + } + + if (PROFILE_ROTATE) { + android.os.Debug.startMethodTracing("/sdcard/launcher-rotate"); + } + return null; + } + + private boolean acceptFilter() { + final InputMethodManager inputManager = (InputMethodManager) + getSystemService(Context.INPUT_METHOD_SERVICE); + return !inputManager.isFullscreenMode(); + } + + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + boolean handled = super.onKeyDown(keyCode, event); + if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) { + boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb, + keyCode, event); + if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) { + // something usable has been typed - start a search + // the typed text will be retrieved and cleared by + // showSearchDialog() + // If there are multiple keystrokes before the search dialog takes focus, + // onSearchRequested() will be called for every keystroke, + // but it is idempotent, so it's fine. + return onSearchRequested(); + } + } + + return handled; + } + + private String getTypedText() { + return mDefaultKeySsb.toString(); + } + + private void clearTypedText() { + mDefaultKeySsb.clear(); + mDefaultKeySsb.clearSpans(); + Selection.setSelection(mDefaultKeySsb, 0); + } + + /** + * Restores the previous state, if it exists. + * + * @param savedState The previous state. + */ + private void restoreState(Bundle savedState) { + if (savedState == null) { + return; + } + + final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1); + if (currentScreen > -1) { + mWorkspace.setCurrentScreen(currentScreen); + } + + final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1); + if (addScreen > -1) { + mAddItemCellInfo = new CellLayout.CellInfo(); + final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo; + addItemCellInfo.valid = true; + addItemCellInfo.screen = addScreen; + addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X); + addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y); + addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X); + addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y); + addItemCellInfo.findVacantCellsFromOccupied( + savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS), + savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X), + savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y)); + mRestoring = true; + } + + boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false); + if (renameFolder) { + long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID); + mFolderInfo = sModel.getFolderById(this, id); + mRestoring = true; + } + } + + /** + * Finds all the views we need and configure them properly. + */ + private void setupViews() { + mDragLayer = (DragLayer) findViewById(R.id.drag_layer); + final DragLayer dragLayer = mDragLayer; + + mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace); + final Workspace workspace = mWorkspace; + + mDrawer = (SlidingDrawer) dragLayer.findViewById(R.id.drawer); + final SlidingDrawer drawer = mDrawer; + + mAllAppsGrid = (AllAppsGridView) drawer.getContent(); + final AllAppsGridView grid = mAllAppsGrid; + + final DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone); + + mHandleView = (HandleView) drawer.findViewById(R.id.all_apps); + mHandleView.setLauncher(this); + mHandleIcon = (TransitionDrawable) mHandleView.getDrawable(); + mHandleIcon.setCrossFadeEnabled(true); + + drawer.lock(); + final DrawerManager drawerManager = new DrawerManager(); + drawer.setOnDrawerOpenListener(drawerManager); + drawer.setOnDrawerCloseListener(drawerManager); + drawer.setOnDrawerScrollListener(drawerManager); + + grid.setTextFilterEnabled(false); + grid.setDragger(dragLayer); + grid.setLauncher(this); + + workspace.setOnLongClickListener(this); + workspace.setDragger(dragLayer); + workspace.setLauncher(this); + loadWallpaper(); + + deleteZone.setLauncher(this); + deleteZone.setDragController(dragLayer); + deleteZone.setHandle(mHandleView); + + dragLayer.setIgnoredDropTarget(grid); + dragLayer.setDragScoller(workspace); + dragLayer.setDragListener(deleteZone); + } + + /** + * Creates a view representing a shortcut. + * + * @param info The data structure describing the shortcut. + * + * @return A View inflated from R.layout.application. + */ + View createShortcut(ApplicationInfo info) { + return createShortcut(R.layout.application, + (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info); + } + + /** + * Creates a view representing a shortcut inflated from the specified resource. + * + * @param layoutResId The id of the XML layout used to create the shortcut. + * @param parent The group the shortcut belongs to. + * @param info The data structure describing the shortcut. + * + * @return A View inflated from layoutResId. + */ + View createShortcut(int layoutResId, ViewGroup parent, ApplicationInfo info) { + TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false); + + if (!info.filtered) { + info.icon = Utilities.createIconThumbnail(info.icon, this); + info.filtered = true; + } + + favorite.setCompoundDrawablesWithIntrinsicBounds(null, info.icon, null, null); + favorite.setText(info.title); + favorite.setTag(info); + favorite.setOnClickListener(this); + + return favorite; + } + + /** + * Add an application shortcut to the workspace. + * + * @param data The intent describing the application. + * @param cellInfo The position on screen where to create the shortcut. + */ + void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo, + boolean insertAtFirst) { + cellInfo.screen = mWorkspace.getCurrentScreen(); + if (!findSingleSlot(cellInfo)) return; + + final ApplicationInfo info = infoFromApplicationIntent(context, data); + if (info != null) { + mWorkspace.addApplicationShortcut(info, cellInfo, insertAtFirst); + } + } + + private static ApplicationInfo infoFromApplicationIntent(Context context, Intent data) { + ComponentName component = data.getComponent(); + PackageManager packageManager = context.getPackageManager(); + ActivityInfo activityInfo = null; + try { + activityInfo = packageManager.getActivityInfo(component, 0 /* no flags */); + } catch (NameNotFoundException e) { + e(LOG_TAG, "Couldn't find ActivityInfo for selected application", e); + } + + if (activityInfo != null) { + ApplicationInfo itemInfo = new ApplicationInfo(); + + itemInfo.title = activityInfo.loadLabel(packageManager); + if (itemInfo.title == null) { + itemInfo.title = activityInfo.name; + } + + itemInfo.setActivity(component, Intent.FLAG_ACTIVITY_NEW_TASK | + Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); + itemInfo.icon = activityInfo.loadIcon(packageManager); + itemInfo.container = ItemInfo.NO_ID; + + return itemInfo; + } + + return null; + } + + /** + * Add a shortcut to the workspace. + * + * @param data The intent describing the shortcut. + * @param cellInfo The position on screen where to create the shortcut. + * @param insertAtFirst + */ + private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo, + boolean insertAtFirst) { + cellInfo.screen = mWorkspace.getCurrentScreen(); + if (!findSingleSlot(cellInfo)) return; + + final ApplicationInfo info = addShortcut(this, data, cellInfo, false); + + if (!mRestoring) { + sModel.addDesktopItem(info); + + final View view = createShortcut(info); + mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst); + } else if (sModel.isDesktopLoaded()) { + sModel.addDesktopItem(info); + } + } + + + /** + * Add a widget to the workspace. + * + * @param data The intent describing the appWidgetId. + * @param cellInfo The position on screen where to create the widget. + */ + private void completeAddAppWidget(Intent data, CellLayout.CellInfo cellInfo, + boolean insertAtFirst) { + + Bundle extras = data.getExtras(); + int appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, -1); + + d(LOG_TAG, "dumping extras content="+extras.toString()); + + AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId); + + // Calculate the grid spans needed to fit this widget + CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen); + int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight); + + // Try finding open space on Launcher screen + final int[] xy = mCellCoordinates; + if (!findSlot(cellInfo, xy, spans[0], spans[1])) return; + + // Build Launcher-specific widget info and save to database + LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId); + launcherInfo.spanX = spans[0]; + launcherInfo.spanY = spans[1]; + + LauncherModel.addItemToDatabase(this, launcherInfo, + LauncherSettings.Favorites.CONTAINER_DESKTOP, + mWorkspace.getCurrentScreen(), xy[0], xy[1], false); + + if (!mRestoring) { + sModel.addDesktopAppWidget(launcherInfo); + + // Perform actual inflation because we're live + launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo); + + launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo); + launcherInfo.hostView.setTag(launcherInfo); + + mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1], + launcherInfo.spanX, launcherInfo.spanY, insertAtFirst); + } else if (sModel.isDesktopLoaded()) { + sModel.addDesktopAppWidget(launcherInfo); + } + } + + public LauncherAppWidgetHost getAppWidgetHost() { + return mAppWidgetHost; + } + + static ApplicationInfo addShortcut(Context context, Intent data, + CellLayout.CellInfo cellInfo, boolean notify) { + + final ApplicationInfo info = infoFromShortcutIntent(context, data); + LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP, + cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify); + + return info; + } + + private static ApplicationInfo infoFromShortcutIntent(Context context, Intent data) { + Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT); + String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); + Bitmap bitmap = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON); + + Drawable icon = null; + boolean filtered = false; + boolean customIcon = false; + ShortcutIconResource iconResource = null; + + if (bitmap != null) { + icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, context)); + filtered = true; + customIcon = true; + } else { + Parcelable extra = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE); + if (extra != null && extra instanceof ShortcutIconResource) { + try { + iconResource = (ShortcutIconResource) extra; + final PackageManager packageManager = context.getPackageManager(); + Resources resources = packageManager.getResourcesForApplication( + iconResource.packageName); + final int id = resources.getIdentifier(iconResource.resourceName, null, null); + icon = resources.getDrawable(id); + } catch (Exception e) { + w(LOG_TAG, "Could not load shortcut icon: " + extra); + } + } + } + + if (icon == null) { + icon = context.getPackageManager().getDefaultActivityIcon(); + } + + final ApplicationInfo info = new ApplicationInfo(); + info.icon = icon; + info.filtered = filtered; + info.title = name; + info.intent = intent; + info.customIcon = customIcon; + info.iconResource = iconResource; + + return info; + } + + @Override + protected void onNewIntent(Intent intent) { + super.onNewIntent(intent); + + // Close the menu + if (Intent.ACTION_MAIN.equals(intent.getAction())) { + getWindow().closeAllPanels(); + + // Set this flag so that onResume knows to close the search dialog if it's open, + // because this was a new intent (thus a press of 'home' or some such) rather than + // for example onResume being called when the user pressed the 'back' button. + mIsNewIntent = true; + + try { + dismissDialog(DIALOG_CREATE_SHORTCUT); + // Unlock the workspace if the dialog was showing + mWorkspace.unlock(); + } catch (Exception e) { + // An exception is thrown if the dialog is not visible, which is fine + } + + try { + dismissDialog(DIALOG_RENAME_FOLDER); + // Unlock the workspace if the dialog was showing + mWorkspace.unlock(); + } catch (Exception e) { + // An exception is thrown if the dialog is not visible, which is fine + } + + if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != + Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) { + + if (!mWorkspace.isDefaultScreenShowing()) { + mWorkspace.moveToDefaultScreen(); + } + + closeDrawer(); + + final View v = getWindow().peekDecorView(); + if (v != null && v.getWindowToken() != null) { + InputMethodManager imm = (InputMethodManager)getSystemService( + INPUT_METHOD_SERVICE); + imm.hideSoftInputFromWindow(v.getWindowToken(), 0); + } + } else { + closeDrawer(false); + } + } + } + + @Override + protected void onRestoreInstanceState(Bundle savedInstanceState) { + // Do not call super here + mSavedInstanceState = savedInstanceState; + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen()); + + final ArrayList<Folder> folders = mWorkspace.getOpenFolders(); + if (folders.size() > 0) { + final int count = folders.size(); + long[] ids = new long[count]; + for (int i = 0; i < count; i++) { + final FolderInfo info = folders.get(i).getInfo(); + ids[i] = info.id; + } + outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids); + } else { + super.onSaveInstanceState(outState); + } + + final boolean isConfigurationChange = getChangingConfigurations() != 0; + + // When the drawer is opened and we are saving the state because of a + // configuration change + if (mDrawer.isOpened() && isConfigurationChange) { + outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true); + } + + if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) { + final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo; + final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen); + + outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen); + outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX); + outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY); + outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX); + outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY); + outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX()); + outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY()); + outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS, + layout.getOccupiedCells()); + } + + if (mFolderInfo != null && mWaitingForResult) { + outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true); + outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id); + } + } + + @Override + public void onDestroy() { + mDestroyed = true; + + super.onDestroy(); + + try { + mAppWidgetHost.stopListening(); + } catch (NullPointerException ex) { + w(LOG_TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex); + } + + TextKeyListener.getInstance().release(); + + mAllAppsGrid.clearTextFilter(); + mAllAppsGrid.setAdapter(null); + sModel.unbind(); + sModel.abortLoaders(); + + getContentResolver().unregisterContentObserver(mObserver); + unregisterReceiver(mApplicationsReceiver); + } + + @Override + public void startActivityForResult(Intent intent, int requestCode) { + if (requestCode >= 0) mWaitingForResult = true; + super.startActivityForResult(intent, requestCode); + } + + @Override + public void startSearch(String initialQuery, boolean selectInitialQuery, + Bundle appSearchData, boolean globalSearch) { + + closeDrawer(false); + + // Slide the search widget to the top, if it's on the current screen, + // otherwise show the search dialog immediately. + Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen(); + if (searchWidget == null) { + showSearchDialog(initialQuery, selectInitialQuery, appSearchData, globalSearch); + } else { + searchWidget.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch); + // show the currently typed text in the search widget while sliding + searchWidget.setQuery(getTypedText()); + } + } + + /** + * Show the search dialog immediately, without changing the search widget. + * + * @see Activity#startSearch(String, boolean, android.os.Bundle, boolean) + */ + void showSearchDialog(String initialQuery, boolean selectInitialQuery, + Bundle appSearchData, boolean globalSearch) { + + if (initialQuery == null) { + // Use any text typed in the launcher as the initial query + initialQuery = getTypedText(); + clearTypedText(); + } + if (appSearchData == null) { + appSearchData = new Bundle(); + appSearchData.putString(SearchManager.SOURCE, "launcher-search"); + } + + final SearchManager searchManager = + (SearchManager) getSystemService(Context.SEARCH_SERVICE); + + final Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen(); + if (searchWidget != null) { + // This gets called when the user leaves the search dialog to go back to + // the Launcher. + searchManager.setOnCancelListener(new SearchManager.OnCancelListener() { + public void onCancel() { + searchManager.setOnCancelListener(null); + stopSearch(); + } + }); + } + + searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(), + appSearchData, globalSearch); + } + + /** + * Cancel search dialog if it is open. + */ + void stopSearch() { + // Close search dialog + SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); + searchManager.stopSearch(); + // Restore search widget to its normal position + Search searchWidget = mWorkspace.findSearchWidgetOnCurrentScreen(); + if (searchWidget != null) { + searchWidget.stopSearch(false); + } + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + if (mDesktopLocked) return false; + + super.onCreateOptionsMenu(menu); + menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add) + .setIcon(android.R.drawable.ic_menu_add) + .setAlphabeticShortcut('A'); + menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper) + .setIcon(android.R.drawable.ic_menu_gallery) + .setAlphabeticShortcut('W'); + menu.add(0, MENU_SEARCH, 0, R.string.menu_search) + .setIcon(android.R.drawable.ic_search_category_default) + .setAlphabeticShortcut(SearchManager.MENU_KEY); + menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications) + .setIcon(com.android.internal.R.drawable.ic_menu_notifications) + .setAlphabeticShortcut('N'); + + final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS); + settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | + Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); + + menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings) + .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P') + .setIntent(settings); + + return true; + } + + @Override + public boolean onPrepareOptionsMenu(Menu menu) { + super.onPrepareOptionsMenu(menu); + + mMenuAddInfo = mWorkspace.findAllVacantCells(null); + menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid); + + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case MENU_ADD: + addItems(); + return true; + case MENU_WALLPAPER_SETTINGS: + startWallpaper(); + return true; + case MENU_SEARCH: + onSearchRequested(); + return true; + case MENU_NOTIFICATIONS: + showNotifications(); + return true; + } + + return super.onOptionsItemSelected(item); + } + + /** + * Indicates that we want global search for this activity by setting the globalSearch + * argument for {@link #startSearch} to true. + */ + + @Override + public boolean onSearchRequested() { + startSearch(null, false, null, true); + return true; + } + + private void addItems() { + showAddDialog(mMenuAddInfo); + } + + private void removeShortcutsForPackage(String packageName) { + if (packageName != null && packageName.length() > 0) { + mWorkspace.removeShortcutsForPackage(packageName); + } + } + + private void updateShortcutsForPackage(String packageName) { + if (packageName != null && packageName.length() > 0) { + mWorkspace.updateShortcutsForPackage(packageName); + } + } + + void addAppWidget(Intent data) { + // TODO: catch bad widget exception when sent + int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1); + + String customWidget = data.getStringExtra(EXTRA_CUSTOM_WIDGET); + if (SEARCH_WIDGET.equals(customWidget)) { + // We don't need this any more, since this isn't a real app widget. + mAppWidgetHost.deleteAppWidgetId(appWidgetId); + // add the search widget + addSearch(); + } else { + AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId); + + if (appWidget.configure != null) { + // Launch over to configure widget, if needed + Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE); + intent.setComponent(appWidget.configure); + intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); + + startActivityForResult(intent, REQUEST_CREATE_APPWIDGET); + } else { + // Otherwise just add it + onActivityResult(REQUEST_CREATE_APPWIDGET, Activity.RESULT_OK, data); + } + } + } + + void addSearch() { + final Widget info = Widget.makeSearch(); + final CellLayout.CellInfo cellInfo = mAddItemCellInfo; + + final int[] xy = mCellCoordinates; + final int spanX = info.spanX; + final int spanY = info.spanY; + + if (!findSlot(cellInfo, xy, spanX, spanY)) return; + + sModel.addDesktopItem(info); + LauncherModel.addItemToDatabase(this, info, LauncherSettings.Favorites.CONTAINER_DESKTOP, + mWorkspace.getCurrentScreen(), xy[0], xy[1], false); + + final View view = mInflater.inflate(info.layoutResource, null); + view.setTag(info); + Search search = (Search) view.findViewById(R.id.widget_search); + search.setLauncher(this); + + mWorkspace.addInCurrentScreen(view, xy[0], xy[1], info.spanX, spanY); + } + + void processShortcut(Intent intent, int requestCodeApplication, int requestCodeShortcut) { + // Handle case where user selected "Applications" + String applicationName = getResources().getString(R.string.group_applications); + String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); + + if (applicationName != null && applicationName.equals(shortcutName)) { + Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); + mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); + + Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY); + pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent); + startActivityForResult(pickIntent, requestCodeApplication); + } else { + startActivityForResult(intent, requestCodeShortcut); + } + } + + void addLiveFolder(Intent intent) { + // Handle case where user selected "Folder" + String folderName = getResources().getString(R.string.group_folder); + String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); + + if (folderName != null && folderName.equals(shortcutName)) { + addFolder(!mDesktopLocked); + } else { + startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER); + } + } + + void addFolder(boolean insertAtFirst) { + UserFolderInfo folderInfo = new UserFolderInfo(); + folderInfo.title = getText(R.string.folder_name); + + CellLayout.CellInfo cellInfo = mAddItemCellInfo; + cellInfo.screen = mWorkspace.getCurrentScreen(); + if (!findSingleSlot(cellInfo)) return; + + // Update the model + LauncherModel.addItemToDatabase(this, folderInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP, + mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false); + sModel.addDesktopItem(folderInfo); + sModel.addFolder(folderInfo); + + // Create the view + FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, + (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo); + mWorkspace.addInCurrentScreen(newFolder, + cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst); + } + + private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo, + boolean insertAtFirst) { + cellInfo.screen = mWorkspace.getCurrentScreen(); + if (!findSingleSlot(cellInfo)) return; + + final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false); + + if (!mRestoring) { + sModel.addDesktopItem(info); + + final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this, + (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info); + mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1, insertAtFirst); + } else if (sModel.isDesktopLoaded()) { + sModel.addDesktopItem(info); + } + } + + static LiveFolderInfo addLiveFolder(Context context, Intent data, + CellLayout.CellInfo cellInfo, boolean notify) { + + Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT); + String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME); + + Drawable icon = null; + boolean filtered = false; + Intent.ShortcutIconResource iconResource = null; + + Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON); + if (extra != null && extra instanceof Intent.ShortcutIconResource) { + try { + iconResource = (Intent.ShortcutIconResource) extra; + final PackageManager packageManager = context.getPackageManager(); + Resources resources = packageManager.getResourcesForApplication( + iconResource.packageName); + final int id = resources.getIdentifier(iconResource.resourceName, null, null); + icon = resources.getDrawable(id); + } catch (Exception e) { + w(LOG_TAG, "Could not load live folder icon: " + extra); + } + } + + if (icon == null) { + icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder); + } + + final LiveFolderInfo info = new LiveFolderInfo(); + info.icon = icon; + info.filtered = filtered; + info.title = name; + info.iconResource = iconResource; + info.uri = data.getData(); + info.baseIntent = baseIntent; + info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE, + LiveFolders.DISPLAY_MODE_GRID); + + LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP, + cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify); + sModel.addFolder(info); + + return info; + } + + private boolean findSingleSlot(CellLayout.CellInfo cellInfo) { + final int[] xy = new int[2]; + if (findSlot(cellInfo, xy, 1, 1)) { + cellInfo.cellX = xy[0]; + cellInfo.cellY = xy[1]; + return true; + } + return false; + } + + private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) { + if (!cellInfo.findCellForSpan(xy, spanX, spanY)) { + boolean[] occupied = mSavedState != null ? + mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null; + cellInfo = mWorkspace.findAllVacantCells(occupied); + if (!cellInfo.findCellForSpan(xy, spanX, spanY)) { + Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show(); + return false; + } + } + return true; + } + + private void showNotifications() { + final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE); + if (statusBar != null) { + statusBar.expand(); + } + } + + private void startWallpaper() { + final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER); + startActivity(Intent.createChooser(pickWallpaper, getString(R.string.chooser_wallpaper))); + } + + /** + * Registers various intent receivers. The current implementation registers + * only a wallpaper intent receiver to let other applications change the + * wallpaper. + */ + private void registerIntentReceivers() { + if (sWallpaperReceiver == null) { + final Application application = getApplication(); + + sWallpaperReceiver = new WallpaperIntentReceiver(application, this); + + IntentFilter filter = new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED); + application.registerReceiver(sWallpaperReceiver, filter); + } else { + sWallpaperReceiver.setLauncher(this); + } + + IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); + filter.addAction(Intent.ACTION_PACKAGE_REMOVED); + filter.addAction(Intent.ACTION_PACKAGE_CHANGED); + filter.addDataScheme("package"); + registerReceiver(mApplicationsReceiver, filter); + } + + /** + * Registers various content observers. The current implementation registers + * only a favorites observer to keep track of the favorites applications. + */ + private void registerContentObservers() { + ContentResolver resolver = getContentResolver(); + resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mObserver); + } + + @Override + public void onWindowFocusChanged(boolean hasFocus) { + super.onWindowFocusChanged(hasFocus); + if (!hasFocus) { + mBackDown = mHomeDown = false; + } + } + + @Override + public boolean dispatchKeyEvent(KeyEvent event) { + if (event.getAction() == KeyEvent.ACTION_DOWN) { + switch (event.getKeyCode()) { + case KeyEvent.KEYCODE_BACK: + mBackDown = true; + return true; + case KeyEvent.KEYCODE_HOME: + mHomeDown = true; + return true; + } + } else if (event.getAction() == KeyEvent.ACTION_UP) { + switch (event.getKeyCode()) { + case KeyEvent.KEYCODE_BACK: + if (!event.isCanceled()) { + mWorkspace.dispatchKeyEvent(event); + if (mDrawer.isOpened()) { + closeDrawer(); + } else { + closeFolder(); + } + } + mBackDown = false; + return true; + case KeyEvent.KEYCODE_HOME: + mHomeDown = false; + return true; + } + } + + return super.dispatchKeyEvent(event); + } + + private void closeDrawer() { + closeDrawer(true); + } + + private void closeDrawer(boolean animated) { + if (mDrawer.isOpened()) { + if (animated) { + mDrawer.animateClose(); + } else { + mDrawer.close(); + } + if (mDrawer.hasFocus()) { + mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus(); + } + } + } + + private void closeFolder() { + Folder folder = mWorkspace.getOpenFolder(); + if (folder != null) { + closeFolder(folder); + } + } + + void closeFolder(Folder folder) { + folder.getInfo().opened = false; + ViewGroup parent = (ViewGroup) folder.getParent(); + if (parent != null) { + parent.removeView(folder); + } + folder.onClose(); + } + + /** + * When the notification that favorites have changed is received, requests + * a favorites list refresh. + */ + private void onFavoritesChanged() { + mDesktopLocked = true; + mDrawer.lock(); + sModel.loadUserItems(false, this, false, false); + } + + void onDesktopItemsLoaded() { + if (mDestroyed) return; + bindDesktopItems(); + } + + /** + * Refreshes the shortcuts shown on the workspace. + */ + private void bindDesktopItems() { + final ArrayList<ItemInfo> shortcuts = sModel.getDesktopItems(); + final ArrayList<LauncherAppWidgetInfo> appWidgets = sModel.getDesktopAppWidgets(); + final ApplicationsAdapter drawerAdapter = sModel.getApplicationsAdapter(); + if (shortcuts == null || appWidgets == null || drawerAdapter == null) { + return; + } + + final Workspace workspace = mWorkspace; + int count = workspace.getChildCount(); + for (int i = 0; i < count; i++) { + ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout(); + } + + if (DEBUG_USER_INTERFACE) { + android.widget.Button finishButton = new android.widget.Button(this); + finishButton.setText("Finish"); + workspace.addInScreen(finishButton, 1, 0, 0, 1, 1); + + finishButton.setOnClickListener(new android.widget.Button.OnClickListener() { + public void onClick(View v) { + finish(); + } + }); + } + + // Flag any old binder to terminate early + if (mBinder != null) { + mBinder.mTerminate = true; + } + + mBinder = new DesktopBinder(this, shortcuts, appWidgets, drawerAdapter); + mBinder.startBindingItems(); + } + + private void bindItems(Launcher.DesktopBinder binder, + ArrayList<ItemInfo> shortcuts, int start, int count) { + + final Workspace workspace = mWorkspace; + final boolean desktopLocked = mDesktopLocked; + + final int end = Math.min(start + DesktopBinder.ITEMS_COUNT, count); + int i = start; + + for ( ; i < end; i++) { + final ItemInfo item = shortcuts.get(i); + switch (item.itemType) { + case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: + case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: + final View shortcut = createShortcut((ApplicationInfo) item); + workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1, + !desktopLocked); + break; + case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER: + final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this, + (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()), + (UserFolderInfo) item); + workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1, + !desktopLocked); + break; + case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER: + final FolderIcon newLiveFolder = LiveFolderIcon.fromXml( + R.layout.live_folder_icon, this, + (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()), + (LiveFolderInfo) item); + workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1, + !desktopLocked); + break; + case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH: + final int screen = workspace.getCurrentScreen(); + final View view = mInflater.inflate(R.layout.widget_search, + (ViewGroup) workspace.getChildAt(screen), false); + + Search search = (Search) view.findViewById(R.id.widget_search); + search.setLauncher(this); + + final Widget widget = (Widget) item; + view.setTag(widget); + + workspace.addWidget(view, widget, !desktopLocked); + break; + } + } + + workspace.requestLayout(); + + if (end >= count) { + finishBindDesktopItems(); + binder.startBindingDrawer(); + } else { + binder.obtainMessage(DesktopBinder.MESSAGE_BIND_ITEMS, i, count).sendToTarget(); + } + } + + private void finishBindDesktopItems() { + if (mSavedState != null) { + if (!mWorkspace.hasFocus()) { + mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus(); + } + + final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS); + if (userFolders != null) { + for (long folderId : userFolders) { + final FolderInfo info = sModel.findFolderById(folderId); + if (info != null) { + openFolder(info); + } + } + final Folder openFolder = mWorkspace.getOpenFolder(); + if (openFolder != null) { + openFolder.requestFocus(); + } + } + + final boolean allApps = mSavedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false); + if (allApps) { + mDrawer.open(); + } + + mSavedState = null; + } + + if (mSavedInstanceState != null) { + super.onRestoreInstanceState(mSavedInstanceState); + mSavedInstanceState = null; + } + + if (mDrawer.isOpened() && !mDrawer.hasFocus()) { + mDrawer.requestFocus(); + } + + mDesktopLocked = false; + mDrawer.unlock(); + } + + private void bindDrawer(Launcher.DesktopBinder binder, + ApplicationsAdapter drawerAdapter) { + mAllAppsGrid.setAdapter(drawerAdapter); + binder.startBindingAppWidgetsWhenIdle(); + } + + private void bindAppWidgets(Launcher.DesktopBinder binder, + LinkedList<LauncherAppWidgetInfo> appWidgets) { + + final Workspace workspace = mWorkspace; + final boolean desktopLocked = mDesktopLocked; + + if (!appWidgets.isEmpty()) { + final LauncherAppWidgetInfo item = appWidgets.removeFirst(); + + final int appWidgetId = item.appWidgetId; + final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId); + item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo); + + if (LOGD) d(LOG_TAG, String.format("about to setAppWidget for id=%d, info=%s", appWidgetId, appWidgetInfo)); + + item.hostView.setAppWidget(appWidgetId, appWidgetInfo); + item.hostView.setTag(item); + + workspace.addInScreen(item.hostView, item.screen, item.cellX, + item.cellY, item.spanX, item.spanY, !desktopLocked); + + workspace.requestLayout(); + } + + if (appWidgets.isEmpty()) { + if (PROFILE_ROTATE) { + android.os.Debug.stopMethodTracing(); + } + } else { + binder.obtainMessage(DesktopBinder.MESSAGE_BIND_APPWIDGETS).sendToTarget(); + } + } + + DragController getDragController() { + return mDragLayer; + } + + /** + * Launches the intent referred by the clicked shortcut. + * + * @param v The view representing the clicked shortcut. + */ + public void onClick(View v) { + Object tag = v.getTag(); + if (tag instanceof ApplicationInfo) { + // Open shortcut + final Intent intent = ((ApplicationInfo) tag).intent; + startActivitySafely(intent); + } else if (tag instanceof FolderInfo) { + handleFolderClick((FolderInfo) tag); + } + } + + void startActivitySafely(Intent intent) { + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + try { + startActivity(intent); + } catch (ActivityNotFoundException e) { + Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); + } catch (SecurityException e) { + Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); + e(LOG_TAG, "Launcher does not have the permission to launch " + intent + + ". Make sure to create a MAIN intent-filter for the corresponding activity " + + "or use the exported attribute for this activity.", e); + } + } + + private void handleFolderClick(FolderInfo folderInfo) { + if (!folderInfo.opened) { + // Close any open folder + closeFolder(); + // Open the requested folder + openFolder(folderInfo); + } else { + // Find the open folder... + Folder openFolder = mWorkspace.getFolderForTag(folderInfo); + int folderScreen; + if (openFolder != null) { + folderScreen = mWorkspace.getScreenForView(openFolder); + // .. and close it + closeFolder(openFolder); + if (folderScreen != mWorkspace.getCurrentScreen()) { + // Close any folder open on the current screen + closeFolder(); + // Pull the folder onto this screen + openFolder(folderInfo); + } + } + } + } + + private void loadWallpaper() { + // The first time the application is started, we load the wallpaper from + // the ApplicationContext + if (sWallpaper == null) { + final Drawable drawable = getWallpaper(); + if (drawable instanceof BitmapDrawable) { + sWallpaper = ((BitmapDrawable) drawable).getBitmap(); + } else { + throw new IllegalStateException("The wallpaper must be a BitmapDrawable."); + } + } + mWorkspace.loadWallpaper(sWallpaper); + } + + /** + * Opens the user fodler described by the specified tag. The opening of the folder + * is animated relative to the specified View. If the View is null, no animation + * is played. + * + * @param folderInfo The FolderInfo describing the folder to open. + */ + private void openFolder(FolderInfo folderInfo) { + Folder openFolder; + + if (folderInfo instanceof UserFolderInfo) { + openFolder = UserFolder.fromXml(this); + } else if (folderInfo instanceof LiveFolderInfo) { + openFolder = com.android.launcher.LiveFolder.fromXml(this, folderInfo); + } else { + return; + } + + openFolder.setDragger(mDragLayer); + openFolder.setLauncher(this); + + openFolder.bind(folderInfo); + folderInfo.opened = true; + + mWorkspace.addInScreen(openFolder, folderInfo.screen, 0, 0, 4, 4); + openFolder.onOpen(); + } + + /** + * Returns true if the workspace is being loaded. When the workspace is loading, + * no user interaction should be allowed to avoid any conflict. + * + * @return True if the workspace is locked, false otherwise. + */ + boolean isWorkspaceLocked() { + return mDesktopLocked; + } + + public boolean onLongClick(View v) { + if (mDesktopLocked) { + return false; + } + + if (!(v instanceof CellLayout)) { + v = (View) v.getParent(); + } + + CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag(); + + // This happens when long clicking an item with the dpad/trackball + if (cellInfo == null) { + return true; + } + + if (mWorkspace.allowLongPress()) { + if (cellInfo.cell == null) { + if (cellInfo.valid) { + // User long pressed on empty space + mWorkspace.setAllowLongPress(false); + showAddDialog(cellInfo); + } + } else { + if (!(cellInfo.cell instanceof Folder)) { + // User long pressed on an item + mWorkspace.startDrag(cellInfo); + } + } + } + return true; + } + + static LauncherModel getModel() { + return sModel; + } + + void closeAllApplications() { + mDrawer.close(); + } + + View getDrawerHandle() { + return mHandleView; + } + + boolean isDrawerDown() { + return !mDrawer.isMoving() && !mDrawer.isOpened(); + } + + boolean isDrawerUp() { + return mDrawer.isOpened() && !mDrawer.isMoving(); + } + + boolean isDrawerMoving() { + return mDrawer.isMoving(); + } + + Workspace getWorkspace() { + return mWorkspace; + } + + GridView getApplicationsGrid() { + return mAllAppsGrid; + } + + @Override + protected Dialog onCreateDialog(int id) { + switch (id) { + case DIALOG_CREATE_SHORTCUT: + return new CreateShortcut().createDialog(); + case DIALOG_RENAME_FOLDER: + return new RenameFolder().createDialog(); + } + + return super.onCreateDialog(id); + } + + @Override + protected void onPrepareDialog(int id, Dialog dialog) { + switch (id) { + case DIALOG_CREATE_SHORTCUT: + break; + case DIALOG_RENAME_FOLDER: + if (mFolderInfo != null) { + EditText input = (EditText) dialog.findViewById(R.id.folder_name); + final CharSequence text = mFolderInfo.title; + input.setText(text); + input.setSelection(0, text.length()); + } + break; + } + } + + void showRenameDialog(FolderInfo info) { + mFolderInfo = info; + mWaitingForResult = true; + showDialog(DIALOG_RENAME_FOLDER); + } + + private void showAddDialog(CellLayout.CellInfo cellInfo) { + mAddItemCellInfo = cellInfo; + mWaitingForResult = true; + showDialog(DIALOG_CREATE_SHORTCUT); + } + + private void pickShortcut(int requestCode, int title) { + Bundle bundle = new Bundle(); + + ArrayList<String> shortcutNames = new ArrayList<String>(); + shortcutNames.add(getString(R.string.group_applications)); + bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames); + + ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>(); + shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this, + R.drawable.ic_launcher_application)); + bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons); + + Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY); + pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT)); + pickIntent.putExtra(Intent.EXTRA_TITLE, getText(title)); + pickIntent.putExtras(bundle); + + startActivityForResult(pickIntent, requestCode); + } + + private class RenameFolder { + private EditText mInput; + + Dialog createDialog() { + mWaitingForResult = true; + final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null); + mInput = (EditText) layout.findViewById(R.id.folder_name); + + AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this); + builder.setIcon(0); + builder.setTitle(getString(R.string.rename_folder_title)); + builder.setCancelable(true); + builder.setOnCancelListener(new Dialog.OnCancelListener() { + public void onCancel(DialogInterface dialog) { + cleanup(); + } + }); + builder.setNegativeButton(getString(R.string.cancel_action), + new Dialog.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + cleanup(); + } + } + ); + builder.setPositiveButton(getString(R.string.rename_action), + new Dialog.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + changeFolderName(); + } + } + ); + builder.setView(layout); + + final AlertDialog dialog = builder.create(); + dialog.setOnShowListener(new DialogInterface.OnShowListener() { + public void onShow(DialogInterface dialog) { + mWorkspace.lock(); + } + }); + + return dialog; + } + + private void changeFolderName() { + final String name = mInput.getText().toString(); + if (!TextUtils.isEmpty(name)) { + // Make sure we have the right folder info + mFolderInfo = sModel.findFolderById(mFolderInfo.id); + mFolderInfo.title = name; + LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo); + + if (mDesktopLocked) { + mDrawer.lock(); + sModel.loadUserItems(false, Launcher.this, false, false); + } else { + final FolderIcon folderIcon = (FolderIcon) + mWorkspace.getViewForTag(mFolderInfo); + if (folderIcon != null) { + folderIcon.setText(name); + getWorkspace().requestLayout(); + } else { + mDesktopLocked = true; + mDrawer.lock(); + sModel.loadUserItems(false, Launcher.this, false, false); + } + } + } + cleanup(); + } + + private void cleanup() { + mWorkspace.unlock(); + dismissDialog(DIALOG_RENAME_FOLDER); + mWaitingForResult = false; + mFolderInfo = null; + } + } + + /** + * Displays the shortcut creation dialog and launches, if necessary, the + * appropriate activity. + */ + private class CreateShortcut implements DialogInterface.OnClickListener, + DialogInterface.OnCancelListener, DialogInterface.OnDismissListener, + DialogInterface.OnShowListener { + + private AddAdapter mAdapter; + + Dialog createDialog() { + mWaitingForResult = true; + + mAdapter = new AddAdapter(Launcher.this); + + final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this); + builder.setTitle(getString(R.string.menu_item_add_item)); + builder.setAdapter(mAdapter, this); + + builder.setInverseBackgroundForced(true); + + AlertDialog dialog = builder.create(); + dialog.setOnCancelListener(this); + dialog.setOnDismissListener(this); + dialog.setOnShowListener(this); + + return dialog; + } + + public void onCancel(DialogInterface dialog) { + mWaitingForResult = false; + cleanup(); + } + + public void onDismiss(DialogInterface dialog) { + mWorkspace.unlock(); + } + + private void cleanup() { + mWorkspace.unlock(); + dismissDialog(DIALOG_CREATE_SHORTCUT); + } + + /** + * Handle the action clicked in the "Add to home" dialog. + */ + public void onClick(DialogInterface dialog, int which) { + Resources res = getResources(); + cleanup(); + + switch (which) { + case AddAdapter.ITEM_SHORTCUT: { + // Insert extra item to handle picking application + pickShortcut(REQUEST_PICK_SHORTCUT, R.string.title_select_shortcut); + break; + } + + case AddAdapter.ITEM_APPWIDGET: { + int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId(); + + Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK); + pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); + // add the search widget + ArrayList<AppWidgetProviderInfo> customInfo = + new ArrayList<AppWidgetProviderInfo>(); + AppWidgetProviderInfo info = new AppWidgetProviderInfo(); + info.provider = new ComponentName(getPackageName(), "XXX.YYY"); + info.label = getString(R.string.group_search); + info.icon = R.drawable.ic_search_widget; + customInfo.add(info); + pickIntent.putParcelableArrayListExtra( + AppWidgetManager.EXTRA_CUSTOM_INFO, customInfo); + ArrayList<Bundle> customExtras = new ArrayList<Bundle>(); + Bundle b = new Bundle(); + b.putString(EXTRA_CUSTOM_WIDGET, SEARCH_WIDGET); + customExtras.add(b); + pickIntent.putParcelableArrayListExtra( + AppWidgetManager.EXTRA_CUSTOM_EXTRAS, customExtras); + // start the pick activity + startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET); + break; + } + + case AddAdapter.ITEM_LIVE_FOLDER: { + // Insert extra item to handle inserting folder + Bundle bundle = new Bundle(); + + ArrayList<String> shortcutNames = new ArrayList<String>(); + shortcutNames.add(res.getString(R.string.group_folder)); + bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames); + + ArrayList<ShortcutIconResource> shortcutIcons = + new ArrayList<ShortcutIconResource>(); + shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this, + R.drawable.ic_launcher_folder)); + bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons); + + Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY); + pickIntent.putExtra(Intent.EXTRA_INTENT, + new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER)); + pickIntent.putExtra(Intent.EXTRA_TITLE, + getText(R.string.title_select_live_folder)); + pickIntent.putExtras(bundle); + + startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER); + break; + } + + case AddAdapter.ITEM_WALLPAPER: { + startWallpaper(); + break; + } + } + } + + public void onShow(DialogInterface dialog) { + mWorkspace.lock(); + } + } + + /** + * Receives notifications when applications are added/removed. + */ + private class ApplicationsIntentReceiver extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + final String action = intent.getAction(); + final String packageName = intent.getData().getSchemeSpecificPart(); + final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); + + if (LauncherModel.DEBUG_LOADERS) { + d(LauncherModel.LOG_TAG, "application intent received: " + action + + ", replacing=" + replacing); + d(LauncherModel.LOG_TAG, " --> " + intent.getData()); + } + + if (!Intent.ACTION_PACKAGE_CHANGED.equals(action)) { + if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) { + if (!replacing) { + removeShortcutsForPackage(packageName); + if (LauncherModel.DEBUG_LOADERS) { + d(LauncherModel.LOG_TAG, " --> remove package"); + } + sModel.removePackage(Launcher.this, packageName); + } + // else, we are replacing the package, so a PACKAGE_ADDED will be sent + // later, we will update the package at this time + } else { + if (!replacing) { + if (LauncherModel.DEBUG_LOADERS) { + d(LauncherModel.LOG_TAG, " --> add package"); + } + sModel.addPackage(Launcher.this, packageName); + } else { + if (LauncherModel.DEBUG_LOADERS) { + d(LauncherModel.LOG_TAG, " --> update package " + packageName); + } + sModel.updatePackage(Launcher.this, packageName); + updateShortcutsForPackage(packageName); + } + } + removeDialog(DIALOG_CREATE_SHORTCUT); + } else { + if (LauncherModel.DEBUG_LOADERS) { + d(LauncherModel.LOG_TAG, " --> sync package " + packageName); + } + sModel.syncPackage(Launcher.this, packageName); + } + } + } + + /** + * Receives notifications whenever the user favorites have changed. + */ + private class FavoritesChangeObserver extends ContentObserver { + public FavoritesChangeObserver() { + super(new Handler()); + } + + @Override + public void onChange(boolean selfChange) { + onFavoritesChanged(); + } + } + + /** + * Receives intents from other applications to change the wallpaper. + */ + private static class WallpaperIntentReceiver extends BroadcastReceiver { + private final Application mApplication; + private WeakReference<Launcher> mLauncher; + + WallpaperIntentReceiver(Application application, Launcher launcher) { + mApplication = application; + setLauncher(launcher); + } + + void setLauncher(Launcher launcher) { + mLauncher = new WeakReference<Launcher>(launcher); + } + + @Override + public void onReceive(Context context, Intent intent) { + // Load the wallpaper from the ApplicationContext and store it locally + // until the Launcher Activity is ready to use it + final Drawable drawable = mApplication.getWallpaper(); + if (drawable instanceof BitmapDrawable) { + sWallpaper = ((BitmapDrawable) drawable).getBitmap(); + } else { + throw new IllegalStateException("The wallpaper must be a BitmapDrawable."); + } + + // If Launcher is alive, notify we have a new wallpaper + if (mLauncher != null) { + final Launcher launcher = mLauncher.get(); + if (launcher != null) { + launcher.loadWallpaper(); + } + } + } + } + + private class DrawerManager implements SlidingDrawer.OnDrawerOpenListener, + SlidingDrawer.OnDrawerCloseListener, SlidingDrawer.OnDrawerScrollListener { + private boolean mOpen; + + public void onDrawerOpened() { + if (!mOpen) { + mHandleIcon.reverseTransition(150); + + final Rect bounds = mWorkspace.mDrawerBounds; + offsetBoundsToDragLayer(bounds, mAllAppsGrid); + + mOpen = true; + } + } + + private void offsetBoundsToDragLayer(Rect bounds, View view) { + view.getDrawingRect(bounds); + + while (view != mDragLayer) { + bounds.offset(view.getLeft(), view.getTop()); + view = (View) view.getParent(); + } + } + + public void onDrawerClosed() { + if (mOpen) { + mHandleIcon.reverseTransition(150); + mWorkspace.mDrawerBounds.setEmpty(); + mOpen = false; + } + + mAllAppsGrid.setSelection(0); + mAllAppsGrid.clearTextFilter(); + } + + public void onScrollStarted() { + if (PROFILE_DRAWER) { + android.os.Debug.startMethodTracing("/sdcard/launcher-drawer"); + } + + mWorkspace.mDrawerContentWidth = mAllAppsGrid.getWidth(); + mWorkspace.mDrawerContentHeight = mAllAppsGrid.getHeight(); + } + + public void onScrollEnded() { + if (PROFILE_DRAWER) { + android.os.Debug.stopMethodTracing(); + } + } + } + + private static class DesktopBinder extends Handler implements MessageQueue.IdleHandler { + static final int MESSAGE_BIND_ITEMS = 0x1; + static final int MESSAGE_BIND_APPWIDGETS = 0x2; + static final int MESSAGE_BIND_DRAWER = 0x3; + + // Number of items to bind in every pass + static final int ITEMS_COUNT = 6; + + private final ArrayList<ItemInfo> mShortcuts; + private final LinkedList<LauncherAppWidgetInfo> mAppWidgets; + private final ApplicationsAdapter mDrawerAdapter; + private final WeakReference<Launcher> mLauncher; + + public boolean mTerminate = false; + + DesktopBinder(Launcher launcher, ArrayList<ItemInfo> shortcuts, + ArrayList<LauncherAppWidgetInfo> appWidgets, + ApplicationsAdapter drawerAdapter) { + + mLauncher = new WeakReference<Launcher>(launcher); + mShortcuts = shortcuts; + mDrawerAdapter = drawerAdapter; + + // Sort widgets so active workspace is bound first + final int currentScreen = launcher.mWorkspace.getCurrentScreen(); + final int size = appWidgets.size(); + mAppWidgets = new LinkedList<LauncherAppWidgetInfo>(); + + for (int i = 0; i < size; i++) { + LauncherAppWidgetInfo appWidgetInfo = appWidgets.get(i); + if (appWidgetInfo.screen == currentScreen) { + mAppWidgets.addFirst(appWidgetInfo); + } else { + mAppWidgets.addLast(appWidgetInfo); + } + } + } + + public void startBindingItems() { + obtainMessage(MESSAGE_BIND_ITEMS, 0, mShortcuts.size()).sendToTarget(); + } + + public void startBindingDrawer() { + obtainMessage(MESSAGE_BIND_DRAWER).sendToTarget(); + } + + public void startBindingAppWidgetsWhenIdle() { + // Ask for notification when message queue becomes idle + final MessageQueue messageQueue = Looper.myQueue(); + messageQueue.addIdleHandler(this); + } + + public boolean queueIdle() { + // Queue is idle, so start binding items + startBindingAppWidgets(); + return false; + } + + public void startBindingAppWidgets() { + obtainMessage(MESSAGE_BIND_APPWIDGETS).sendToTarget(); + } + + @Override + public void handleMessage(Message msg) { + Launcher launcher = mLauncher.get(); + if (launcher == null || mTerminate) { + return; + } + + switch (msg.what) { + case MESSAGE_BIND_ITEMS: { + launcher.bindItems(this, mShortcuts, msg.arg1, msg.arg2); + break; + } + case MESSAGE_BIND_DRAWER: { + launcher.bindDrawer(this, mDrawerAdapter); + break; + } + case MESSAGE_BIND_APPWIDGETS: { + launcher.bindAppWidgets(this, mAppWidgets); + break; + } + } + } + } +} diff --git a/src/com/android/launcher2/LauncherAppWidgetHost.java b/src/com/android/launcher2/LauncherAppWidgetHost.java new file mode 100644 index 0000000..22fd5b6 --- /dev/null +++ b/src/com/android/launcher2/LauncherAppWidgetHost.java @@ -0,0 +1,39 @@ +/* + * 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.launcher; + +import android.appwidget.AppWidgetHost; +import android.appwidget.AppWidgetHostView; +import android.appwidget.AppWidgetProviderInfo; +import android.content.Context; + +/** + * 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. + */ +public class LauncherAppWidgetHost extends AppWidgetHost { + public LauncherAppWidgetHost(Context context, int hostId) { + super(context, hostId); + } + + @Override + protected AppWidgetHostView onCreateView(Context context, int appWidgetId, + AppWidgetProviderInfo appWidget) { + return new LauncherAppWidgetHostView(context); + } +} diff --git a/src/com/android/launcher2/LauncherAppWidgetHostView.java b/src/com/android/launcher2/LauncherAppWidgetHostView.java new file mode 100644 index 0000000..da5b3a0 --- /dev/null +++ b/src/com/android/launcher2/LauncherAppWidgetHostView.java @@ -0,0 +1,111 @@ +/* + * 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.launcher; + +import android.appwidget.AppWidgetHostView; +import android.content.Context; +import android.view.LayoutInflater; +import android.view.MotionEvent; +import android.view.View; +import android.view.ViewConfiguration; + +/** + * {@inheritDoc} + */ +public class LauncherAppWidgetHostView extends AppWidgetHostView { + private boolean mHasPerformedLongPress; + + private CheckForLongPress mPendingCheckForLongPress; + + private LayoutInflater mInflater; + + public LauncherAppWidgetHostView(Context context) { + super(context); + mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + } + + @Override + protected View getErrorView() { + return mInflater.inflate(R.layout.appwidget_error, this, false); + } + + public boolean onInterceptTouchEvent(MotionEvent ev) { + // Consume any touch events for ourselves after longpress is triggered + if (mHasPerformedLongPress) { + mHasPerformedLongPress = false; + return true; + } + + // Watch for longpress events at this level to make sure + // users can always pick up this widget + switch (ev.getAction()) { + case MotionEvent.ACTION_DOWN: { + postCheckForLongClick(); + break; + } + + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_CANCEL: + mHasPerformedLongPress = false; + if (mPendingCheckForLongPress != null) { + removeCallbacks(mPendingCheckForLongPress); + } + break; + } + + // Otherwise continue letting touch events fall through to children + return false; + } + + class CheckForLongPress implements Runnable { + private int mOriginalWindowAttachCount; + + public void run() { + if ((mParent != null) && hasWindowFocus() + && mOriginalWindowAttachCount == getWindowAttachCount() + && !mHasPerformedLongPress) { + if (performLongClick()) { + mHasPerformedLongPress = true; + } + } + } + + public void rememberWindowAttachCount() { + mOriginalWindowAttachCount = getWindowAttachCount(); + } + } + + private void postCheckForLongClick() { + mHasPerformedLongPress = false; + + if (mPendingCheckForLongPress == null) { + mPendingCheckForLongPress = new CheckForLongPress(); + } + mPendingCheckForLongPress.rememberWindowAttachCount(); + postDelayed(mPendingCheckForLongPress, ViewConfiguration.getLongPressTimeout()); + } + + @Override + public void cancelLongPress() { + super.cancelLongPress(); + + mHasPerformedLongPress = false; + if (mPendingCheckForLongPress != null) { + removeCallbacks(mPendingCheckForLongPress); + } + } +} diff --git a/src/com/android/launcher2/LauncherAppWidgetInfo.java b/src/com/android/launcher2/LauncherAppWidgetInfo.java new file mode 100644 index 0000000..3b5f08e --- /dev/null +++ b/src/com/android/launcher2/LauncherAppWidgetInfo.java @@ -0,0 +1,53 @@ +/* + * 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.launcher; + +import android.appwidget.AppWidgetHostView; +import android.content.ContentValues; + +/** + * Represents a widget, which just contains an identifier. + */ +class LauncherAppWidgetInfo extends ItemInfo { + + /** + * Identifier for this widget when talking with {@link AppWidgetManager} for updates. + */ + int appWidgetId; + + /** + * View that holds this widget after it's been created. This view isn't created + * until Launcher knows it's needed. + */ + AppWidgetHostView hostView = null; + + LauncherAppWidgetInfo(int appWidgetId) { + itemType = LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET; + this.appWidgetId = appWidgetId; + } + + @Override + void onAddToDatabase(ContentValues values) { + super.onAddToDatabase(values); + values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId); + } + + @Override + public String toString() { + return Integer.toString(appWidgetId); + } +} diff --git a/src/com/android/launcher2/LauncherApplication.java b/src/com/android/launcher2/LauncherApplication.java new file mode 100644 index 0000000..d71fa19 --- /dev/null +++ b/src/com/android/launcher2/LauncherApplication.java @@ -0,0 +1,29 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.app.Application; +import dalvik.system.VMRuntime; + +public class LauncherApplication extends Application { + @Override + public void onCreate() { + VMRuntime.getRuntime().setMinimumHeapSize(4 * 1024 * 1024); + + super.onCreate(); + } +} diff --git a/src/com/android/launcher2/LauncherModel.java b/src/com/android/launcher2/LauncherModel.java new file mode 100644 index 0000000..591463b --- /dev/null +++ b/src/com/android/launcher2/LauncherModel.java @@ -0,0 +1,1482 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.content.ComponentName; +import android.content.ContentResolver; +import android.content.ContentValues; +import android.content.Intent; +import android.content.Context; +import android.content.pm.ActivityInfo; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.content.res.Resources; +import android.database.Cursor; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.drawable.Drawable; +import android.net.Uri; +import static android.util.Log.*; +import android.os.Process; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Comparator; +import java.lang.ref.WeakReference; +import java.text.Collator; +import java.net.URISyntaxException; + +/** + * Maintains in-memory state of the Launcher. It is expected that there should be only one + * LauncherModel object held in a static. Also provide APIs for updating the database state + * for the Launcher. + */ +public class LauncherModel { + static final boolean DEBUG_LOADERS = true; + static final String LOG_TAG = "HomeLoaders"; + + private static final int UI_NOTIFICATION_RATE = 4; + private static final int DEFAULT_APPLICATIONS_NUMBER = 42; + private static final long APPLICATION_NOT_RESPONDING_TIMEOUT = 5000; + private static final int INITIAL_ICON_CACHE_CAPACITY = 50; + + private static final Collator sCollator = Collator.getInstance(); + + private boolean mApplicationsLoaded; + private boolean mDesktopItemsLoaded; + + private ArrayList<ItemInfo> mDesktopItems; + private ArrayList<LauncherAppWidgetInfo> mDesktopAppWidgets; + private HashMap<Long, FolderInfo> mFolders; + + private ArrayList<ApplicationInfo> mApplications; + private ApplicationsAdapter mApplicationsAdapter; + private ApplicationsLoader mApplicationsLoader; + private DesktopItemsLoader mDesktopItemsLoader; + private Thread mApplicationsLoaderThread; + private Thread mDesktopLoaderThread; + + private final HashMap<ComponentName, ApplicationInfo> mAppInfoCache = + new HashMap<ComponentName, ApplicationInfo>(INITIAL_ICON_CACHE_CAPACITY); + + synchronized void abortLoaders() { + if (mApplicationsLoader != null && mApplicationsLoader.isRunning()) { + mApplicationsLoader.stop(); + mApplicationsLoaded = false; + } + + if (mDesktopItemsLoader != null && mDesktopItemsLoader.isRunning()) { + mDesktopItemsLoader.stop(); + mDesktopItemsLoaded = false; + } + } + + /** + * Drop our cache of components to their lables & icons. We do + * this from Launcher when applications are added/removed. It's a + * bit overkill, but it's a rare operation anyway. + */ + synchronized void dropApplicationCache() { + mAppInfoCache.clear(); + } + + /** + * Loads the list of installed applications in mApplications. + * + * @return true if the applications loader must be started + * (see startApplicationsLoader()), false otherwise. + */ + synchronized boolean loadApplications(boolean isLaunching, Launcher launcher, + boolean localeChanged) { + + if (DEBUG_LOADERS) d(LOG_TAG, "load applications"); + + if (isLaunching && mApplicationsLoaded && !localeChanged) { + mApplicationsAdapter = new ApplicationsAdapter(launcher, mApplications); + if (DEBUG_LOADERS) d(LOG_TAG, " --> applications loaded, return"); + return false; + } + + stopAndWaitForApplicationsLoader(); + + if (localeChanged) { + dropApplicationCache(); + } + + if (mApplicationsAdapter == null || isLaunching || localeChanged) { + mApplications = new ArrayList<ApplicationInfo>(DEFAULT_APPLICATIONS_NUMBER); + mApplicationsAdapter = new ApplicationsAdapter(launcher, mApplications); + } + + mApplicationsLoaded = false; + + if (!isLaunching) { + startApplicationsLoader(launcher, false); + return false; + } + + return true; + } + + private synchronized void stopAndWaitForApplicationsLoader() { + if (mApplicationsLoader != null && mApplicationsLoader.isRunning()) { + if (DEBUG_LOADERS) d(LOG_TAG, " --> wait for applications loader"); + + mApplicationsLoader.stop(); + // Wait for the currently running thread to finish, this can take a little + // time but it should be well below the timeout limit + try { + mApplicationsLoaderThread.join(APPLICATION_NOT_RESPONDING_TIMEOUT); + } catch (InterruptedException e) { + // EMpty + } + } + } + + private synchronized void startApplicationsLoader(Launcher launcher, boolean isLaunching) { + if (DEBUG_LOADERS) d(LOG_TAG, " --> starting applications loader"); + + stopAndWaitForApplicationsLoader(); + + mApplicationsLoader = new ApplicationsLoader(launcher, isLaunching); + mApplicationsLoaderThread = new Thread(mApplicationsLoader, "Applications Loader"); + mApplicationsLoaderThread.start(); + } + + synchronized void addPackage(Launcher launcher, String packageName) { + if (mApplicationsLoader != null && mApplicationsLoader.isRunning()) { + startApplicationsLoader(launcher, false); + return; + } + + if (packageName != null && packageName.length() > 0) { + final PackageManager packageManager = launcher.getPackageManager(); + final List<ResolveInfo> matches = findActivitiesForPackage(packageManager, packageName); + + if (matches.size() > 0) { + final ApplicationsAdapter adapter = mApplicationsAdapter; + final HashMap<ComponentName, ApplicationInfo> cache = mAppInfoCache; + + for (ResolveInfo info : matches) { + adapter.setNotifyOnChange(false); + adapter.add(makeAndCacheApplicationInfo(packageManager, cache, info, launcher)); + } + + adapter.sort(new ApplicationInfoComparator()); + adapter.notifyDataSetChanged(); + } + } + } + + synchronized void removePackage(Launcher launcher, String packageName) { + if (mApplicationsLoader != null && mApplicationsLoader.isRunning()) { + dropApplicationCache(); // TODO: this could be optimized + startApplicationsLoader(launcher, false); + return; + } + + if (packageName != null && packageName.length() > 0) { + final ApplicationsAdapter adapter = mApplicationsAdapter; + + final List<ApplicationInfo> toRemove = new ArrayList<ApplicationInfo>(); + final int count = adapter.getCount(); + + for (int i = 0; i < count; i++) { + final ApplicationInfo applicationInfo = adapter.getItem(i); + final Intent intent = applicationInfo.intent; + final ComponentName component = intent.getComponent(); + if (packageName.equals(component.getPackageName())) { + toRemove.add(applicationInfo); + } + } + + final HashMap<ComponentName, ApplicationInfo> cache = mAppInfoCache; + for (ApplicationInfo info : toRemove) { + adapter.setNotifyOnChange(false); + adapter.remove(info); + cache.remove(info.intent.getComponent()); + } + + if (toRemove.size() > 0) { + adapter.sort(new ApplicationInfoComparator()); + adapter.notifyDataSetChanged(); + } + } + } + + synchronized void updatePackage(Launcher launcher, String packageName) { + if (mApplicationsLoader != null && mApplicationsLoader.isRunning()) { + startApplicationsLoader(launcher, false); + return; + } + + if (packageName != null && packageName.length() > 0) { + final PackageManager packageManager = launcher.getPackageManager(); + final ApplicationsAdapter adapter = mApplicationsAdapter; + + final List<ResolveInfo> matches = findActivitiesForPackage(packageManager, packageName); + final int count = matches.size(); + + boolean changed = false; + + for (int i = 0; i < count; i++) { + final ResolveInfo info = matches.get(i); + final ApplicationInfo applicationInfo = findIntent(adapter, + info.activityInfo.applicationInfo.packageName, info.activityInfo.name); + if (applicationInfo != null) { + updateAndCacheApplicationInfo(packageManager, info, applicationInfo, launcher); + changed = true; + } + } + + if (syncLocked(launcher, packageName)) changed = true; + + if (changed) { + adapter.sort(new ApplicationInfoComparator()); + adapter.notifyDataSetChanged(); + } + } + } + + private void updateAndCacheApplicationInfo(PackageManager packageManager, ResolveInfo info, + ApplicationInfo applicationInfo, Context context) { + + updateApplicationInfoTitleAndIcon(packageManager, info, applicationInfo, context); + + ComponentName componentName = new ComponentName( + info.activityInfo.applicationInfo.packageName, info.activityInfo.name); + mAppInfoCache.put(componentName, applicationInfo); + } + + synchronized void syncPackage(Launcher launcher, String packageName) { + if (mApplicationsLoader != null && mApplicationsLoader.isRunning()) { + startApplicationsLoader(launcher, false); + return; + } + + if (packageName != null && packageName.length() > 0) { + if (syncLocked(launcher, packageName)) { + final ApplicationsAdapter adapter = mApplicationsAdapter; + adapter.sort(new ApplicationInfoComparator()); + adapter.notifyDataSetChanged(); + } + } + } + + private boolean syncLocked(Launcher launcher, String packageName) { + final PackageManager packageManager = launcher.getPackageManager(); + final List<ResolveInfo> matches = findActivitiesForPackage(packageManager, packageName); + + if (matches.size() > 0) { + final ApplicationsAdapter adapter = mApplicationsAdapter; + + // Find disabled activities and remove them from the adapter + boolean removed = removeDisabledActivities(packageName, matches, adapter); + // Find enable activities and add them to the adapter + // Also updates existing activities with new labels/icons + boolean added = addEnabledAndUpdateActivities(matches, adapter, launcher); + + return added || removed; + } + + return false; + } + + private static List<ResolveInfo> findActivitiesForPackage(PackageManager packageManager, + String packageName) { + + final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); + mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); + + final List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0); + final List<ResolveInfo> matches = new ArrayList<ResolveInfo>(); + + if (apps != null) { + // Find all activities that match the packageName + int count = apps.size(); + for (int i = 0; i < count; i++) { + final ResolveInfo info = apps.get(i); + final ActivityInfo activityInfo = info.activityInfo; + if (packageName.equals(activityInfo.packageName)) { + matches.add(info); + } + } + } + + return matches; + } + + private boolean addEnabledAndUpdateActivities(List<ResolveInfo> matches, + ApplicationsAdapter adapter, Launcher launcher) { + + final List<ApplicationInfo> toAdd = new ArrayList<ApplicationInfo>(); + final int count = matches.size(); + + boolean changed = false; + + for (int i = 0; i < count; i++) { + final ResolveInfo info = matches.get(i); + final ApplicationInfo applicationInfo = findIntent(adapter, + info.activityInfo.applicationInfo.packageName, info.activityInfo.name); + if (applicationInfo == null) { + toAdd.add(makeAndCacheApplicationInfo(launcher.getPackageManager(), + mAppInfoCache, info, launcher)); + changed = true; + } else { + updateAndCacheApplicationInfo( + launcher.getPackageManager(), info, applicationInfo, launcher); + changed = true; + } + } + + for (ApplicationInfo info : toAdd) { + adapter.setNotifyOnChange(false); + adapter.add(info); + } + + return changed; + } + + private boolean removeDisabledActivities(String packageName, List<ResolveInfo> matches, + ApplicationsAdapter adapter) { + + final List<ApplicationInfo> toRemove = new ArrayList<ApplicationInfo>(); + final int count = adapter.getCount(); + + boolean changed = false; + + for (int i = 0; i < count; i++) { + final ApplicationInfo applicationInfo = adapter.getItem(i); + final Intent intent = applicationInfo.intent; + final ComponentName component = intent.getComponent(); + if (packageName.equals(component.getPackageName())) { + if (!findIntent(matches, component)) { + toRemove.add(applicationInfo); + changed = true; + } + } + } + + final HashMap<ComponentName, ApplicationInfo> cache = mAppInfoCache; + for (ApplicationInfo info : toRemove) { + adapter.setNotifyOnChange(false); + adapter.remove(info); + cache.remove(info.intent.getComponent()); + } + + return changed; + } + + private static ApplicationInfo findIntent(ApplicationsAdapter adapter, String packageName, + String name) { + + final int count = adapter.getCount(); + for (int i = 0; i < count; i++) { + final ApplicationInfo applicationInfo = adapter.getItem(i); + final Intent intent = applicationInfo.intent; + final ComponentName component = intent.getComponent(); + if (packageName.equals(component.getPackageName()) && + name.equals(component.getClassName())) { + return applicationInfo; + } + } + + return null; + } + + private static boolean findIntent(List<ResolveInfo> apps, ComponentName component) { + final String className = component.getClassName(); + for (ResolveInfo info : apps) { + final ActivityInfo activityInfo = info.activityInfo; + if (activityInfo.name.equals(className)) { + return true; + } + } + return false; + } + + Drawable getApplicationInfoIcon(PackageManager manager, ApplicationInfo info) { + final ResolveInfo resolveInfo = manager.resolveActivity(info.intent, 0); + if (resolveInfo == null) { + return null; + } + + ComponentName componentName = new ComponentName( + resolveInfo.activityInfo.applicationInfo.packageName, + resolveInfo.activityInfo.name); + ApplicationInfo application = mAppInfoCache.get(componentName); + + if (application == null) { + return resolveInfo.activityInfo.loadIcon(manager); + } + + return application.icon; + } + + private static ApplicationInfo makeAndCacheApplicationInfo(PackageManager manager, + HashMap<ComponentName, ApplicationInfo> appInfoCache, ResolveInfo info, + Context context) { + + ComponentName componentName = new ComponentName( + info.activityInfo.applicationInfo.packageName, + info.activityInfo.name); + ApplicationInfo application = appInfoCache.get(componentName); + + if (application == null) { + application = new ApplicationInfo(); + application.container = ItemInfo.NO_ID; + + updateApplicationInfoTitleAndIcon(manager, info, application, context); + + application.setActivity(componentName, + Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); + + appInfoCache.put(componentName, application); + } + + return application; + } + + private static void updateApplicationInfoTitleAndIcon(PackageManager manager, ResolveInfo info, + ApplicationInfo application, Context context) { + + application.title = info.loadLabel(manager); + if (application.title == null) { + application.title = info.activityInfo.name; + } + + application.icon = + Utilities.createIconThumbnail(info.activityInfo.loadIcon(manager), context); + application.filtered = false; + } + + private class ApplicationsLoader implements Runnable { + private final WeakReference<Launcher> mLauncher; + + private volatile boolean mStopped; + private volatile boolean mRunning; + private final boolean mIsLaunching; + + ApplicationsLoader(Launcher launcher, boolean isLaunching) { + mIsLaunching = isLaunching; + mLauncher = new WeakReference<Launcher>(launcher); + } + + void stop() { + mStopped = true; + } + + boolean isRunning() { + return mRunning; + } + + public void run() { + mRunning = true; + + // Elevate priority when Home launches for the first time to avoid + // starving at boot time. Staring at a blank home is not cool. + android.os.Process.setThreadPriority(mIsLaunching ? Process.THREAD_PRIORITY_DEFAULT : + Process.THREAD_PRIORITY_BACKGROUND); + + final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); + mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); + + final Launcher launcher = mLauncher.get(); + final PackageManager manager = launcher.getPackageManager(); + final List<ResolveInfo> apps = manager.queryIntentActivities(mainIntent, 0); + + if (apps != null && !mStopped) { + final int count = apps.size(); + // Can be set to null on the UI thread by the unbind() method + // Do not access without checking for null first + final ApplicationsAdapter applicationList = mApplicationsAdapter; + + ChangeNotifier action = new ChangeNotifier(applicationList, true); + final HashMap<ComponentName, ApplicationInfo> appInfoCache = mAppInfoCache; + + for (int i = 0; i < count && !mStopped; i++) { + ResolveInfo info = apps.get(i); + ApplicationInfo application = + makeAndCacheApplicationInfo(manager, appInfoCache, info, launcher); + + if (action.add(application) && !mStopped) { + launcher.runOnUiThread(action); + action = new ChangeNotifier(applicationList, false); + } + } + + launcher.runOnUiThread(action); + } + + if (!mStopped) { + mApplicationsLoaded = true; + } + mRunning = false; + } + } + + private static class ChangeNotifier implements Runnable { + private final ApplicationsAdapter mApplicationList; + private final ArrayList<ApplicationInfo> mBuffer; + + private boolean mFirst = true; + + ChangeNotifier(ApplicationsAdapter applicationList, boolean first) { + mApplicationList = applicationList; + mFirst = first; + mBuffer = new ArrayList<ApplicationInfo>(UI_NOTIFICATION_RATE); + } + + public void run() { + final ApplicationsAdapter applicationList = mApplicationList; + // Can be set to null on the UI thread by the unbind() method + if (applicationList == null) return; + + if (mFirst) { + applicationList.setNotifyOnChange(false); + applicationList.clear(); + mFirst = false; + } + + final ArrayList<ApplicationInfo> buffer = mBuffer; + final int count = buffer.size(); + + for (int i = 0; i < count; i++) { + applicationList.setNotifyOnChange(false); + applicationList.add(buffer.get(i)); + } + + buffer.clear(); + + applicationList.sort(new ApplicationInfoComparator()); + applicationList.notifyDataSetChanged(); + } + + boolean add(ApplicationInfo application) { + final ArrayList<ApplicationInfo> buffer = mBuffer; + buffer.add(application); + return buffer.size() >= UI_NOTIFICATION_RATE; + } + } + + static class ApplicationInfoComparator implements Comparator<ApplicationInfo> { + public final int compare(ApplicationInfo a, ApplicationInfo b) { + return sCollator.compare(a.title.toString(), b.title.toString()); + } + } + + boolean isDesktopLoaded() { + return mDesktopItems != null && mDesktopAppWidgets != null && mDesktopItemsLoaded; + } + + /** + * Loads all of the items on the desktop, in folders, or in the dock. + * These can be apps, shortcuts or widgets + */ + void loadUserItems(boolean isLaunching, Launcher launcher, boolean localeChanged, + boolean loadApplications) { + if (DEBUG_LOADERS) d(LOG_TAG, "loading user items"); + + if (isLaunching && isDesktopLoaded()) { + if (DEBUG_LOADERS) d(LOG_TAG, " --> items loaded, return"); + if (loadApplications) startApplicationsLoader(launcher, true); + // We have already loaded our data from the DB + launcher.onDesktopItemsLoaded(); + return; + } + + if (mDesktopItemsLoader != null && mDesktopItemsLoader.isRunning()) { + if (DEBUG_LOADERS) d(LOG_TAG, " --> stopping workspace loader"); + mDesktopItemsLoader.stop(); + // Wait for the currently running thread to finish, this can take a little + // time but it should be well below the timeout limit + try { + mDesktopLoaderThread.join(APPLICATION_NOT_RESPONDING_TIMEOUT); + } catch (InterruptedException e) { + // Empty + } + + // If the thread we are interrupting was tasked to load the list of + // applications make sure we keep that information in the new loader + // spawned below + // note: we don't apply this to localeChanged because the thread can + // only be stopped *after* the localeChanged handling has occured + loadApplications = mDesktopItemsLoader.mLoadApplications; + } + + if (DEBUG_LOADERS) d(LOG_TAG, " --> starting workspace loader"); + mDesktopItemsLoaded = false; + mDesktopItemsLoader = new DesktopItemsLoader(launcher, localeChanged, loadApplications, + isLaunching); + mDesktopLoaderThread = new Thread(mDesktopItemsLoader, "Desktop Items Loader"); + mDesktopLoaderThread.start(); + } + + private static void updateShortcutLabels(ContentResolver resolver, PackageManager manager) { + final Cursor c = resolver.query(LauncherSettings.Favorites.CONTENT_URI, + new String[] { LauncherSettings.Favorites._ID, LauncherSettings.Favorites.TITLE, + LauncherSettings.Favorites.INTENT, LauncherSettings.Favorites.ITEM_TYPE }, + null, null, null); + + final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID); + final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT); + final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE); + final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE); + + // boolean changed = false; + + try { + while (c.moveToNext()) { + try { + if (c.getInt(itemTypeIndex) != + LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) { + continue; + } + + final String intentUri = c.getString(intentIndex); + if (intentUri != null) { + final Intent shortcut = Intent.parseUri(intentUri, 0); + if (Intent.ACTION_MAIN.equals(shortcut.getAction())) { + final ComponentName name = shortcut.getComponent(); + if (name != null) { + final ActivityInfo activityInfo = manager.getActivityInfo(name, 0); + final String title = c.getString(titleIndex); + String label = getLabel(manager, activityInfo); + + if (title == null || !title.equals(label)) { + final ContentValues values = new ContentValues(); + values.put(LauncherSettings.Favorites.TITLE, label); + + resolver.update( + LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, + values, "_id=?", + new String[] { String.valueOf(c.getLong(idIndex)) }); + + // changed = true; + } + } + } + } + } catch (URISyntaxException e) { + // Ignore + } catch (PackageManager.NameNotFoundException e) { + // Ignore + } + } + } finally { + c.close(); + } + + // if (changed) resolver.notifyChange(Settings.Favorites.CONTENT_URI, null); + } + + private static String getLabel(PackageManager manager, ActivityInfo activityInfo) { + String label = activityInfo.loadLabel(manager).toString(); + if (label == null) { + label = manager.getApplicationLabel(activityInfo.applicationInfo).toString(); + if (label == null) { + label = activityInfo.name; + } + } + return label; + } + + private class DesktopItemsLoader implements Runnable { + private volatile boolean mStopped; + private volatile boolean mRunning; + + private final WeakReference<Launcher> mLauncher; + private final boolean mLocaleChanged; + private final boolean mLoadApplications; + private final boolean mIsLaunching; + + DesktopItemsLoader(Launcher launcher, boolean localeChanged, boolean loadApplications, + boolean isLaunching) { + mLoadApplications = loadApplications; + mIsLaunching = isLaunching; + mLauncher = new WeakReference<Launcher>(launcher); + mLocaleChanged = localeChanged; + } + + void stop() { + mStopped = true; + } + + boolean isRunning() { + return mRunning; + } + + public void run() { + mRunning = true; + + android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); + + final Launcher launcher = mLauncher.get(); + final ContentResolver contentResolver = launcher.getContentResolver(); + final PackageManager manager = launcher.getPackageManager(); + + if (mLocaleChanged) { + updateShortcutLabels(contentResolver, manager); + } + + mDesktopItems = new ArrayList<ItemInfo>(); + mDesktopAppWidgets = new ArrayList<LauncherAppWidgetInfo>(); + mFolders = new HashMap<Long, FolderInfo>(); + + final ArrayList<ItemInfo> desktopItems = mDesktopItems; + final ArrayList<LauncherAppWidgetInfo> desktopAppWidgets = mDesktopAppWidgets; + + final Cursor c = contentResolver.query( + LauncherSettings.Favorites.CONTENT_URI, null, null, null, null); + + try { + final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID); + final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT); + final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE); + final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE); + final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON); + final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE); + final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE); + final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER); + final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE); + final int appWidgetIdIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.APPWIDGET_ID); + final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN); + final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX); + final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY); + final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX); + final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY); + final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI); + final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE); + + ApplicationInfo info; + String intentDescription; + Widget widgetInfo; + LauncherAppWidgetInfo appWidgetInfo; + int container; + long id; + Intent intent; + + final HashMap<Long, FolderInfo> folders = mFolders; + + while (!mStopped && c.moveToNext()) { + try { + int itemType = c.getInt(itemTypeIndex); + + switch (itemType) { + case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: + case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: + intentDescription = c.getString(intentIndex); + try { + intent = Intent.parseUri(intentDescription, 0); + } catch (java.net.URISyntaxException e) { + continue; + } + + if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) { + info = getApplicationInfo(manager, intent, launcher); + } else { + info = getApplicationInfoShortcut(c, launcher, iconTypeIndex, + iconPackageIndex, iconResourceIndex, iconIndex); + } + + if (info == null) { + info = new ApplicationInfo(); + info.icon = manager.getDefaultActivityIcon(); + } + + if (info != null) { + info.title = c.getString(titleIndex); + info.intent = intent; + + info.id = c.getLong(idIndex); + container = c.getInt(containerIndex); + info.container = container; + info.screen = c.getInt(screenIndex); + info.cellX = c.getInt(cellXIndex); + info.cellY = c.getInt(cellYIndex); + + switch (container) { + case LauncherSettings.Favorites.CONTAINER_DESKTOP: + desktopItems.add(info); + break; + default: + // Item is in a user folder + UserFolderInfo folderInfo = + findOrMakeUserFolder(folders, container); + folderInfo.add(info); + break; + } + } + break; + case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER: + + id = c.getLong(idIndex); + UserFolderInfo folderInfo = findOrMakeUserFolder(folders, id); + + folderInfo.title = c.getString(titleIndex); + + folderInfo.id = id; + container = c.getInt(containerIndex); + folderInfo.container = container; + folderInfo.screen = c.getInt(screenIndex); + folderInfo.cellX = c.getInt(cellXIndex); + folderInfo.cellY = c.getInt(cellYIndex); + + switch (container) { + case LauncherSettings.Favorites.CONTAINER_DESKTOP: + desktopItems.add(folderInfo); + break; + } + break; + case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER: + + id = c.getLong(idIndex); + LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(folders, id); + + intentDescription = c.getString(intentIndex); + intent = null; + if (intentDescription != null) { + try { + intent = Intent.parseUri(intentDescription, 0); + } catch (java.net.URISyntaxException e) { + // Ignore, a live folder might not have a base intent + } + } + + liveFolderInfo.title = c.getString(titleIndex); + liveFolderInfo.id = id; + container = c.getInt(containerIndex); + liveFolderInfo.container = container; + liveFolderInfo.screen = c.getInt(screenIndex); + liveFolderInfo.cellX = c.getInt(cellXIndex); + liveFolderInfo.cellY = c.getInt(cellYIndex); + liveFolderInfo.uri = Uri.parse(c.getString(uriIndex)); + liveFolderInfo.baseIntent = intent; + liveFolderInfo.displayMode = c.getInt(displayModeIndex); + + loadLiveFolderIcon(launcher, c, iconTypeIndex, iconPackageIndex, + iconResourceIndex, liveFolderInfo); + + switch (container) { + case LauncherSettings.Favorites.CONTAINER_DESKTOP: + desktopItems.add(liveFolderInfo); + break; + } + break; + case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH: + widgetInfo = Widget.makeSearch(); + + container = c.getInt(containerIndex); + if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) { + e(Launcher.LOG_TAG, "Widget found where container " + + "!= CONTAINER_DESKTOP ignoring!"); + continue; + } + + widgetInfo.id = c.getLong(idIndex); + widgetInfo.screen = c.getInt(screenIndex); + widgetInfo.container = container; + widgetInfo.cellX = c.getInt(cellXIndex); + widgetInfo.cellY = c.getInt(cellYIndex); + + desktopItems.add(widgetInfo); + break; + case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET: + // Read all Launcher-specific widget details + int appWidgetId = c.getInt(appWidgetIdIndex); + appWidgetInfo = new LauncherAppWidgetInfo(appWidgetId); + appWidgetInfo.id = c.getLong(idIndex); + appWidgetInfo.screen = c.getInt(screenIndex); + appWidgetInfo.cellX = c.getInt(cellXIndex); + appWidgetInfo.cellY = c.getInt(cellYIndex); + appWidgetInfo.spanX = c.getInt(spanXIndex); + appWidgetInfo.spanY = c.getInt(spanYIndex); + + container = c.getInt(containerIndex); + if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) { + e(Launcher.LOG_TAG, "Widget found where container " + + "!= CONTAINER_DESKTOP -- ignoring!"); + continue; + } + appWidgetInfo.container = c.getInt(containerIndex); + + desktopAppWidgets.add(appWidgetInfo); + break; + } + } catch (Exception e) { + w(Launcher.LOG_TAG, "Desktop items loading interrupted:", e); + } + } + } finally { + c.close(); + } + + if (!mStopped) { + launcher.runOnUiThread(new Runnable() { + public void run() { + launcher.onDesktopItemsLoaded(); + } + }); + if (mLoadApplications) startApplicationsLoader(launcher, mIsLaunching); + } + + if (!mStopped) { + mDesktopItemsLoaded = true; + } + mRunning = false; + } + } + + private static void loadLiveFolderIcon(Launcher launcher, Cursor c, int iconTypeIndex, + int iconPackageIndex, int iconResourceIndex, LiveFolderInfo liveFolderInfo) { + + int iconType = c.getInt(iconTypeIndex); + switch (iconType) { + case LauncherSettings.Favorites.ICON_TYPE_RESOURCE: + String packageName = c.getString(iconPackageIndex); + String resourceName = c.getString(iconResourceIndex); + PackageManager packageManager = launcher.getPackageManager(); + try { + Resources resources = packageManager.getResourcesForApplication(packageName); + final int id = resources.getIdentifier(resourceName, null, null); + liveFolderInfo.icon = resources.getDrawable(id); + } catch (Exception e) { + liveFolderInfo.icon = + launcher.getResources().getDrawable(R.drawable.ic_launcher_folder); + } + liveFolderInfo.iconResource = new Intent.ShortcutIconResource(); + liveFolderInfo.iconResource.packageName = packageName; + liveFolderInfo.iconResource.resourceName = resourceName; + break; + default: + liveFolderInfo.icon = + launcher.getResources().getDrawable(R.drawable.ic_launcher_folder); + } + } + + /** + * Finds the user folder defined by the specified id. + * + * @param id The id of the folder to look for. + * + * @return A UserFolderInfo if the folder exists or null otherwise. + */ + FolderInfo findFolderById(long id) { + return mFolders.get(id); + } + + void addFolder(FolderInfo info) { + mFolders.put(info.id, info); + } + + /** + * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a + * new one. + */ + private UserFolderInfo findOrMakeUserFolder(HashMap<Long, FolderInfo> folders, long id) { + // See if a placeholder was created for us already + FolderInfo folderInfo = folders.get(id); + if (folderInfo == null || !(folderInfo instanceof UserFolderInfo)) { + // No placeholder -- create a new instance + folderInfo = new UserFolderInfo(); + folders.put(id, folderInfo); + } + return (UserFolderInfo) folderInfo; + } + + /** + * Return an existing UserFolderInfo object if we have encountered this ID previously, or make a + * new one. + */ + private LiveFolderInfo findOrMakeLiveFolder(HashMap<Long, FolderInfo> folders, long id) { + // See if a placeholder was created for us already + FolderInfo folderInfo = folders.get(id); + if (folderInfo == null || !(folderInfo instanceof LiveFolderInfo)) { + // No placeholder -- create a new instance + folderInfo = new LiveFolderInfo(); + folders.put(id, folderInfo); + } + return (LiveFolderInfo) folderInfo; + } + + /** + * Remove the callback for the cached drawables or we leak the previous + * Home screen on orientation change. + */ + void unbind() { + // Interrupt the applications loader before setting the adapter to null + stopAndWaitForApplicationsLoader(); + mApplicationsAdapter = null; + unbindAppDrawables(mApplications); + unbindDrawables(mDesktopItems); + unbindAppWidgetHostViews(mDesktopAppWidgets); + unbindCachedIconDrawables(); + } + + /** + * Remove the callback for the cached drawables or we leak the previous + * Home screen on orientation change. + */ + private void unbindDrawables(ArrayList<ItemInfo> desktopItems) { + if (desktopItems != null) { + final int count = desktopItems.size(); + for (int i = 0; i < count; i++) { + ItemInfo item = desktopItems.get(i); + switch (item.itemType) { + case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: + case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: + ((ApplicationInfo)item).icon.setCallback(null); + break; + } + } + } + } + + /** + * Remove the callback for the cached drawables or we leak the previous + * Home screen on orientation change. + */ + private void unbindAppDrawables(ArrayList<ApplicationInfo> applications) { + if (applications != null) { + final int count = applications.size(); + for (int i = 0; i < count; i++) { + applications.get(i).icon.setCallback(null); + } + } + } + + /** + * Remove any {@link LauncherAppWidgetHostView} references in our widgets. + */ + private void unbindAppWidgetHostViews(ArrayList<LauncherAppWidgetInfo> appWidgets) { + if (appWidgets != null) { + final int count = appWidgets.size(); + for (int i = 0; i < count; i++) { + LauncherAppWidgetInfo launcherInfo = appWidgets.get(i); + launcherInfo.hostView = null; + } + } + } + + /** + * Remove the callback for the cached drawables or we leak the previous + * Home screen on orientation change. + */ + private void unbindCachedIconDrawables() { + for (ApplicationInfo appInfo : mAppInfoCache.values()) { + appInfo.icon.setCallback(null); + } + } + + /** + * @return The current list of applications + */ + ApplicationsAdapter getApplicationsAdapter() { + return mApplicationsAdapter; + } + + /** + * @return The current list of desktop items + */ + ArrayList<ItemInfo> getDesktopItems() { + return mDesktopItems; + } + + /** + * @return The current list of desktop items + */ + ArrayList<LauncherAppWidgetInfo> getDesktopAppWidgets() { + return mDesktopAppWidgets; + } + + /** + * Add an item to the desktop + * @param info + */ + void addDesktopItem(ItemInfo info) { + // TODO: write to DB; also check that folder has been added to folders list + mDesktopItems.add(info); + } + + /** + * Remove an item from the desktop + * @param info + */ + void removeDesktopItem(ItemInfo info) { + // TODO: write to DB; figure out if we should remove folder from folders list + mDesktopItems.remove(info); + } + + /** + * Add a widget to the desktop + */ + void addDesktopAppWidget(LauncherAppWidgetInfo info) { + mDesktopAppWidgets.add(info); + } + + /** + * Remove a widget from the desktop + */ + void removeDesktopAppWidget(LauncherAppWidgetInfo info) { + mDesktopAppWidgets.remove(info); + } + + /** + * Make an ApplicationInfo object for an application + */ + private static ApplicationInfo getApplicationInfo(PackageManager manager, Intent intent, + Context context) { + final ResolveInfo resolveInfo = manager.resolveActivity(intent, 0); + + if (resolveInfo == null) { + return null; + } + + final ApplicationInfo info = new ApplicationInfo(); + final ActivityInfo activityInfo = resolveInfo.activityInfo; + info.icon = Utilities.createIconThumbnail(activityInfo.loadIcon(manager), context); + if (info.title == null || info.title.length() == 0) { + info.title = activityInfo.loadLabel(manager); + } + if (info.title == null) { + info.title = ""; + } + info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION; + return info; + } + + /** + * Make an ApplicationInfo object for a sortcut + */ + private ApplicationInfo getApplicationInfoShortcut(Cursor c, Context context, + int iconTypeIndex, int iconPackageIndex, int iconResourceIndex, int iconIndex) { + + final ApplicationInfo info = new ApplicationInfo(); + info.itemType = LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT; + + int iconType = c.getInt(iconTypeIndex); + switch (iconType) { + case LauncherSettings.Favorites.ICON_TYPE_RESOURCE: + String packageName = c.getString(iconPackageIndex); + String resourceName = c.getString(iconResourceIndex); + PackageManager packageManager = context.getPackageManager(); + try { + Resources resources = packageManager.getResourcesForApplication(packageName); + final int id = resources.getIdentifier(resourceName, null, null); + info.icon = Utilities.createIconThumbnail(resources.getDrawable(id), context); + } catch (Exception e) { + info.icon = packageManager.getDefaultActivityIcon(); + } + info.iconResource = new Intent.ShortcutIconResource(); + info.iconResource.packageName = packageName; + info.iconResource.resourceName = resourceName; + info.customIcon = false; + break; + case LauncherSettings.Favorites.ICON_TYPE_BITMAP: + byte[] data = c.getBlob(iconIndex); + try { + Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); + info.icon = new FastBitmapDrawable( + Utilities.createBitmapThumbnail(bitmap, context)); + } catch (Exception e) { + packageManager = context.getPackageManager(); + info.icon = packageManager.getDefaultActivityIcon(); + } + info.filtered = true; + info.customIcon = true; + break; + default: + info.icon = context.getPackageManager().getDefaultActivityIcon(); + info.customIcon = false; + break; + } + return info; + } + + /** + * Remove an item from the in-memory represention of a user folder. Does not change the DB. + */ + void removeUserFolderItem(UserFolderInfo folder, ItemInfo info) { + //noinspection SuspiciousMethodCalls + folder.contents.remove(info); + } + + /** + * Removes a UserFolder from the in-memory list of folders. Does not change the DB. + * @param userFolderInfo + */ + void removeUserFolder(UserFolderInfo userFolderInfo) { + mFolders.remove(userFolderInfo.id); + } + + /** + * Adds an item to the DB if it was not created previously, or move it to a new + * <container, screen, cellX, cellY> + */ + static void addOrMoveItemInDatabase(Context context, ItemInfo item, long container, + int screen, int cellX, int cellY) { + if (item.container == ItemInfo.NO_ID) { + // From all apps + addItemToDatabase(context, item, container, screen, cellX, cellY, false); + } else { + // From somewhere else + moveItemInDatabase(context, item, container, screen, cellX, cellY); + } + } + + /** + * Move an item in the DB to a new <container, screen, cellX, cellY> + */ + static void moveItemInDatabase(Context context, ItemInfo item, long container, int screen, + int cellX, int cellY) { + item.container = container; + item.screen = screen; + item.cellX = cellX; + item.cellY = cellY; + + final ContentValues values = new ContentValues(); + final ContentResolver cr = context.getContentResolver(); + + values.put(LauncherSettings.Favorites.CONTAINER, item.container); + values.put(LauncherSettings.Favorites.CELLX, item.cellX); + values.put(LauncherSettings.Favorites.CELLY, item.cellY); + values.put(LauncherSettings.Favorites.SCREEN, item.screen); + + cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null); + } + + /** + * Returns true if the shortcuts already exists in the database. + * we identify a shortcut by its title and intent. + */ + static boolean shortcutExists(Context context, String title, Intent intent) { + final ContentResolver cr = context.getContentResolver(); + Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, + new String[] { "title", "intent" }, "title=? and intent=?", + new String[] { title, intent.toUri(0) }, null); + boolean result = false; + try { + result = c.moveToFirst(); + } finally { + c.close(); + } + return result; + } + + FolderInfo getFolderById(Context context, long id) { + final ContentResolver cr = context.getContentResolver(); + Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, null, + "_id=? and (itemType=? or itemType=?)", + new String[] { String.valueOf(id), + String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER), + String.valueOf(LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER) }, null); + + try { + if (c.moveToFirst()) { + final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE); + final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE); + final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER); + final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN); + final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX); + final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY); + + FolderInfo folderInfo = null; + switch (c.getInt(itemTypeIndex)) { + case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER: + folderInfo = findOrMakeUserFolder(mFolders, id); + break; + case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER: + folderInfo = findOrMakeLiveFolder(mFolders, id); + break; + } + + folderInfo.title = c.getString(titleIndex); + folderInfo.id = id; + folderInfo.container = c.getInt(containerIndex); + folderInfo.screen = c.getInt(screenIndex); + folderInfo.cellX = c.getInt(cellXIndex); + folderInfo.cellY = c.getInt(cellYIndex); + + return folderInfo; + } + } finally { + c.close(); + } + + return null; + } + + /** + * Add an item to the database in a specified container. Sets the container, screen, cellX and + * cellY fields of the item. Also assigns an ID to the item. + */ + static void addItemToDatabase(Context context, ItemInfo item, long container, + int screen, int cellX, int cellY, boolean notify) { + item.container = container; + item.screen = screen; + item.cellX = cellX; + item.cellY = cellY; + + final ContentValues values = new ContentValues(); + final ContentResolver cr = context.getContentResolver(); + + item.onAddToDatabase(values); + + Uri result = cr.insert(notify ? LauncherSettings.Favorites.CONTENT_URI : + LauncherSettings.Favorites.CONTENT_URI_NO_NOTIFICATION, values); + + if (result != null) { + item.id = Integer.parseInt(result.getPathSegments().get(1)); + } + } + + /** + * Add an item to the database in a specified container. Sets the container, screen, cellX and + * cellY fields of the item. Also assigns an ID to the item. + */ + static boolean addGestureToDatabase(Context context, ItemInfo item, boolean notify) { + final ContentValues values = new ContentValues(); + final ContentResolver cr = context.getContentResolver(); + + item.onAddToDatabase(values); + + Uri result = cr.insert(notify ? LauncherSettings.Gestures.CONTENT_URI : + LauncherSettings.Gestures.CONTENT_URI_NO_NOTIFICATION, values); + + if (result != null) { + item.id = Integer.parseInt(result.getPathSegments().get(1)); + } + + return result != null; + } + + /** + * Update an item to the database in a specified container. + */ + static void updateItemInDatabase(Context context, ItemInfo item) { + final ContentValues values = new ContentValues(); + final ContentResolver cr = context.getContentResolver(); + + item.onAddToDatabase(values); + + cr.update(LauncherSettings.Favorites.getContentUri(item.id, false), values, null, null); + } + + /** + * Removes the specified item from the database + * @param context + * @param item + */ + static void deleteItemFromDatabase(Context context, ItemInfo item) { + final ContentResolver cr = context.getContentResolver(); + + cr.delete(LauncherSettings.Favorites.getContentUri(item.id, false), null, null); + } + + + /** + * Remove the contents of the specified folder from the database + */ + static void deleteUserFolderContentsFromDatabase(Context context, UserFolderInfo info) { + final ContentResolver cr = context.getContentResolver(); + + cr.delete(LauncherSettings.Favorites.getContentUri(info.id, false), null, null); + cr.delete(LauncherSettings.Favorites.CONTENT_URI, + LauncherSettings.Favorites.CONTAINER + "=" + info.id, null); + } + + static void deleteGestureFromDatabase(Context context, ItemInfo item) { + final ContentResolver cr = context.getContentResolver(); + + cr.delete(LauncherSettings.Gestures.getContentUri(item.id, false), null, null); + } + + static void updateGestureInDatabase(Context context, ItemInfo item) { + final ContentValues values = new ContentValues(); + final ContentResolver cr = context.getContentResolver(); + + item.onAddToDatabase(values); + + cr.update(LauncherSettings.Gestures.getContentUri(item.id, false), values, null, null); + } + + + ApplicationInfo queryGesture(Context context, String id) { + final ContentResolver contentResolver = context.getContentResolver(); + final PackageManager manager = context.getPackageManager(); + final Cursor c = contentResolver.query( + LauncherSettings.Gestures.CONTENT_URI, null, LauncherSettings.Gestures._ID + "=?", + new String[] { id }, null); + + ApplicationInfo info = null; + + try { + final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Gestures._ID); + final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Gestures.INTENT); + final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Gestures.TITLE); + final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Gestures.ICON_TYPE); + final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Gestures.ICON); + final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Gestures.ICON_PACKAGE); + final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Gestures.ICON_RESOURCE); + final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Gestures.ITEM_TYPE); + + String intentDescription; + Intent intent; + + if (c.moveToNext()) { + int itemType = c.getInt(itemTypeIndex); + + switch (itemType) { + case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: + case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: + intentDescription = c.getString(intentIndex); + try { + intent = Intent.parseUri(intentDescription, 0); + } catch (java.net.URISyntaxException e) { + return null; + } + + if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) { + info = getApplicationInfo(manager, intent, context); + } else { + info = getApplicationInfoShortcut(c, context, iconTypeIndex, + iconPackageIndex, iconResourceIndex, iconIndex); + } + + if (info == null) { + info = new ApplicationInfo(); + info.icon = manager.getDefaultActivityIcon(); + } + + info.isGesture = true; + info.title = c.getString(titleIndex); + info.intent = intent; + info.id = c.getLong(idIndex); + + break; + } + } + } catch (Exception e) { + w(LOG_TAG, "Could not load gesture with name " + id); + } finally { + c.close(); + } + + return info; + } +} diff --git a/src/com/android/launcher2/LauncherProvider.java b/src/com/android/launcher2/LauncherProvider.java new file mode 100644 index 0000000..fc38cdd --- /dev/null +++ b/src/com/android/launcher2/LauncherProvider.java @@ -0,0 +1,671 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.appwidget.AppWidgetHost; +import android.content.ContentProvider; +import android.content.Context; +import android.content.ContentValues; +import android.content.Intent; +import android.content.ComponentName; +import android.content.ContentUris; +import android.content.ContentResolver; +import android.content.res.XmlResourceParser; +import android.content.res.TypedArray; +import android.content.pm.PackageManager; +import android.content.pm.ActivityInfo; +import android.database.sqlite.SQLiteOpenHelper; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteQueryBuilder; +import android.database.Cursor; +import android.database.SQLException; +import android.util.Log; +import android.util.Xml; +import android.util.AttributeSet; +import android.net.Uri; +import android.text.TextUtils; +import android.os.*; +import android.provider.Settings; + +import java.io.IOException; +import java.util.ArrayList; + +import org.xmlpull.v1.XmlPullParserException; +import org.xmlpull.v1.XmlPullParser; +import com.android.internal.util.XmlUtils; +import com.android.launcher.LauncherSettings.Favorites; + +public class LauncherProvider extends ContentProvider { + private static final String LOG_TAG = "LauncherProvider"; + private static final boolean LOGD = true; + + private static final String DATABASE_NAME = "launcher.db"; + + private static final int DATABASE_VERSION = 4; + + static final String AUTHORITY = "com.android.launcher.settings"; + + static final String EXTRA_BIND_SOURCES = "com.android.launcher.settings.bindsources"; + static final String EXTRA_BIND_TARGETS = "com.android.launcher.settings.bindtargets"; + + static final String TABLE_FAVORITES = "favorites"; + static final String TABLE_GESTURES = "gestures"; + static final String PARAMETER_NOTIFY = "notify"; + + /** + * {@link Uri} triggered at any registered {@link android.database.ContentObserver} when + * {@link AppWidgetHost#deleteHost()} is called during database creation. + * Use this to recall {@link AppWidgetHost#startListening()} if needed. + */ + static final Uri CONTENT_APPWIDGET_RESET_URI = + Uri.parse("content://" + AUTHORITY + "/appWidgetReset"); + + private SQLiteOpenHelper mOpenHelper; + + @Override + public boolean onCreate() { + mOpenHelper = new DatabaseHelper(getContext()); + return true; + } + + @Override + public String getType(Uri uri) { + SqlArguments args = new SqlArguments(uri, null, null); + if (TextUtils.isEmpty(args.where)) { + return "vnd.android.cursor.dir/" + args.table; + } else { + return "vnd.android.cursor.item/" + args.table; + } + } + + @Override + public Cursor query(Uri uri, String[] projection, String selection, + String[] selectionArgs, String sortOrder) { + + SqlArguments args = new SqlArguments(uri, selection, selectionArgs); + SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); + qb.setTables(args.table); + + SQLiteDatabase db = mOpenHelper.getWritableDatabase(); + Cursor result = qb.query(db, projection, args.where, args.args, null, null, sortOrder); + result.setNotificationUri(getContext().getContentResolver(), uri); + + return result; + } + + @Override + public Uri insert(Uri uri, ContentValues initialValues) { + SqlArguments args = new SqlArguments(uri); + + SQLiteDatabase db = mOpenHelper.getWritableDatabase(); + final long rowId = db.insert(args.table, null, initialValues); + if (rowId <= 0) return null; + + uri = ContentUris.withAppendedId(uri, rowId); + sendNotify(uri); + + return uri; + } + + @Override + public int bulkInsert(Uri uri, ContentValues[] values) { + SqlArguments args = new SqlArguments(uri); + + SQLiteDatabase db = mOpenHelper.getWritableDatabase(); + db.beginTransaction(); + try { + int numValues = values.length; + for (int i = 0; i < numValues; i++) { + if (db.insert(args.table, null, values[i]) < 0) return 0; + } + db.setTransactionSuccessful(); + } finally { + db.endTransaction(); + } + + sendNotify(uri); + return values.length; + } + + @Override + public int delete(Uri uri, String selection, String[] selectionArgs) { + SqlArguments args = new SqlArguments(uri, selection, selectionArgs); + + SQLiteDatabase db = mOpenHelper.getWritableDatabase(); + int count = db.delete(args.table, args.where, args.args); + if (count > 0) sendNotify(uri); + + return count; + } + + @Override + public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { + SqlArguments args = new SqlArguments(uri, selection, selectionArgs); + + SQLiteDatabase db = mOpenHelper.getWritableDatabase(); + int count = db.update(args.table, values, args.where, args.args); + if (count > 0) sendNotify(uri); + + return count; + } + + private void sendNotify(Uri uri) { + String notify = uri.getQueryParameter(PARAMETER_NOTIFY); + if (notify == null || "true".equals(notify)) { + getContext().getContentResolver().notifyChange(uri, null); + } + } + + private static class DatabaseHelper extends SQLiteOpenHelper { + private static final String TAG_FAVORITES = "favorites"; + private static final String TAG_FAVORITE = "favorite"; + private static final String TAG_CLOCK = "clock"; + private static final String TAG_SEARCH = "search"; + + private final Context mContext; + private final AppWidgetHost mAppWidgetHost; + + DatabaseHelper(Context context) { + super(context, DATABASE_NAME, null, DATABASE_VERSION); + mContext = context; + mAppWidgetHost = new AppWidgetHost(context, Launcher.APPWIDGET_HOST_ID); + } + + /** + * Send notification that we've deleted the {@link AppWidgetHost}, + * probably as part of the initial database creation. The receiver may + * want to re-call {@link AppWidgetHost#startListening()} to ensure + * callbacks are correctly set. + */ + private void sendAppWidgetResetNotify() { + final ContentResolver resolver = mContext.getContentResolver(); + resolver.notifyChange(CONTENT_APPWIDGET_RESET_URI, null); + } + + @Override + public void onCreate(SQLiteDatabase db) { + if (LOGD) Log.d(LOG_TAG, "creating new launcher database"); + + db.execSQL("CREATE TABLE favorites (" + + "_id INTEGER PRIMARY KEY," + + "title TEXT," + + "intent TEXT," + + "container INTEGER," + + "screen INTEGER," + + "cellX INTEGER," + + "cellY INTEGER," + + "spanX INTEGER," + + "spanY INTEGER," + + "itemType INTEGER," + + "appWidgetId INTEGER NOT NULL DEFAULT -1," + + "isShortcut INTEGER," + + "iconType INTEGER," + + "iconPackage TEXT," + + "iconResource TEXT," + + "icon BLOB," + + "uri TEXT," + + "displayMode INTEGER" + + ");"); + + db.execSQL("CREATE TABLE gestures (" + + "_id INTEGER PRIMARY KEY," + + "title TEXT," + + "intent TEXT," + + "itemType INTEGER," + + "iconType INTEGER," + + "iconPackage TEXT," + + "iconResource TEXT," + + "icon BLOB" + + ");"); + + // Database was just created, so wipe any previous widgets + if (mAppWidgetHost != null) { + mAppWidgetHost.deleteHost(); + sendAppWidgetResetNotify(); + } + + if (!convertDatabase(db)) { + // Populate favorites table with initial favorites + loadFavorites(db); + } + } + + private boolean convertDatabase(SQLiteDatabase db) { + if (LOGD) Log.d(LOG_TAG, "converting database from an older format, but not onUpgrade"); + boolean converted = false; + + final Uri uri = Uri.parse("content://" + Settings.AUTHORITY + + "/old_favorites?notify=true"); + final ContentResolver resolver = mContext.getContentResolver(); + Cursor cursor = null; + + try { + cursor = resolver.query(uri, null, null, null, null); + } catch (Exception e) { + // Ignore + } + + // We already have a favorites database in the old provider + if (cursor != null && cursor.getCount() > 0) { + try { + converted = copyFromCursor(db, cursor) > 0; + } finally { + cursor.close(); + } + + if (converted) { + resolver.delete(uri, null, null); + } + } + + if (converted) { + // Convert widgets from this import into widgets + if (LOGD) Log.d(LOG_TAG, "converted and now triggering widget upgrade"); + convertWidgets(db); + } + + return converted; + } + + private int copyFromCursor(SQLiteDatabase db, Cursor c) { + final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID); + final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT); + final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE); + final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE); + final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON); + final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE); + final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE); + final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER); + final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE); + final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN); + final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX); + final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY); + final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI); + final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE); + + ContentValues[] rows = new ContentValues[c.getCount()]; + int i = 0; + while (c.moveToNext()) { + ContentValues values = new ContentValues(c.getColumnCount()); + values.put(LauncherSettings.Favorites._ID, c.getLong(idIndex)); + values.put(LauncherSettings.Favorites.INTENT, c.getString(intentIndex)); + values.put(LauncherSettings.Favorites.TITLE, c.getString(titleIndex)); + values.put(LauncherSettings.Favorites.ICON_TYPE, c.getInt(iconTypeIndex)); + values.put(LauncherSettings.Favorites.ICON, c.getBlob(iconIndex)); + values.put(LauncherSettings.Favorites.ICON_PACKAGE, c.getString(iconPackageIndex)); + values.put(LauncherSettings.Favorites.ICON_RESOURCE, c.getString(iconResourceIndex)); + values.put(LauncherSettings.Favorites.CONTAINER, c.getInt(containerIndex)); + values.put(LauncherSettings.Favorites.ITEM_TYPE, c.getInt(itemTypeIndex)); + values.put(LauncherSettings.Favorites.APPWIDGET_ID, -1); + values.put(LauncherSettings.Favorites.SCREEN, c.getInt(screenIndex)); + values.put(LauncherSettings.Favorites.CELLX, c.getInt(cellXIndex)); + values.put(LauncherSettings.Favorites.CELLY, c.getInt(cellYIndex)); + values.put(LauncherSettings.Favorites.URI, c.getString(uriIndex)); + values.put(LauncherSettings.Favorites.DISPLAY_MODE, c.getInt(displayModeIndex)); + rows[i++] = values; + } + + db.beginTransaction(); + int total = 0; + try { + int numValues = rows.length; + for (i = 0; i < numValues; i++) { + if (db.insert(TABLE_FAVORITES, null, rows[i]) < 0) { + return 0; + } else { + total++; + } + } + db.setTransactionSuccessful(); + } finally { + db.endTransaction(); + } + + return total; + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + if (LOGD) Log.d(LOG_TAG, "onUpgrade triggered"); + + int version = oldVersion; + if (version < 3) { + // upgrade 1,2 -> 3 added appWidgetId column + db.beginTransaction(); + try { + // Insert new column for holding appWidgetIds + db.execSQL("ALTER TABLE favorites " + + "ADD COLUMN appWidgetId INTEGER NOT NULL DEFAULT -1;"); + db.setTransactionSuccessful(); + version = 3; + } catch (SQLException ex) { + // Old version remains, which means we wipe old data + Log.e(LOG_TAG, ex.getMessage(), ex); + } finally { + db.endTransaction(); + } + + // Convert existing widgets only if table upgrade was successful + if (version == 3) { + convertWidgets(db); + } + } + + if (version < 4) { + db.beginTransaction(); + try { + db.execSQL("CREATE TABLE gestures (" + + "_id INTEGER PRIMARY KEY," + + "title TEXT," + + "intent TEXT," + + "itemType INTEGER," + + "iconType INTEGER," + + "iconPackage TEXT," + + "iconResource TEXT," + + "icon BLOB" + + ");"); + db.setTransactionSuccessful(); + version = 4; + } catch (SQLException ex) { + // Old version remains, which means we wipe old data + Log.e(LOG_TAG, ex.getMessage(), ex); + } finally { + db.endTransaction(); + } + } + + if (version != DATABASE_VERSION) { + Log.w(LOG_TAG, "Destroying all old data."); + db.execSQL("DROP TABLE IF EXISTS " + TABLE_FAVORITES); + db.execSQL("DROP TABLE IF EXISTS " + TABLE_GESTURES); + onCreate(db); + } + } + + /** + * Upgrade existing clock and photo frame widgets into their new widget + * equivalents. This method allocates appWidgetIds, and then hands off to + * LauncherAppWidgetBinder to finish the actual binding. + */ + private void convertWidgets(SQLiteDatabase db) { + final int[] bindSources = new int[] { + Favorites.ITEM_TYPE_WIDGET_CLOCK, + Favorites.ITEM_TYPE_WIDGET_PHOTO_FRAME, + }; + + final ArrayList<ComponentName> bindTargets = new ArrayList<ComponentName>(); + bindTargets.add(new ComponentName("com.android.alarmclock", + "com.android.alarmclock.AnalogAppWidgetProvider")); + bindTargets.add(new ComponentName("com.android.camera", + "com.android.camera.PhotoAppWidgetProvider")); + + final String selectWhere = buildOrWhereString(Favorites.ITEM_TYPE, bindSources); + + Cursor c = null; + boolean allocatedAppWidgets = false; + + db.beginTransaction(); + try { + // Select and iterate through each matching widget + c = db.query(TABLE_FAVORITES, new String[] { Favorites._ID }, + selectWhere, null, null, null, null); + + if (LOGD) Log.d(LOG_TAG, "found upgrade cursor count="+c.getCount()); + + final ContentValues values = new ContentValues(); + while (c != null && c.moveToNext()) { + long favoriteId = c.getLong(0); + + // Allocate and update database with new appWidgetId + try { + int appWidgetId = mAppWidgetHost.allocateAppWidgetId(); + + if (LOGD) Log.d(LOG_TAG, "allocated appWidgetId="+appWidgetId+" for favoriteId="+favoriteId); + + values.clear(); + values.put(LauncherSettings.Favorites.APPWIDGET_ID, appWidgetId); + + // Original widgets might not have valid spans when upgrading + values.put(LauncherSettings.Favorites.SPANX, 2); + values.put(LauncherSettings.Favorites.SPANY, 2); + + String updateWhere = Favorites._ID + "=" + favoriteId; + db.update(TABLE_FAVORITES, values, updateWhere, null); + + allocatedAppWidgets = true; + } catch (RuntimeException ex) { + Log.e(LOG_TAG, "Problem allocating appWidgetId", ex); + } + } + + db.setTransactionSuccessful(); + } catch (SQLException ex) { + Log.w(LOG_TAG, "Problem while allocating appWidgetIds for existing widgets", ex); + } finally { + db.endTransaction(); + if (c != null) { + c.close(); + } + } + + // If any appWidgetIds allocated, then launch over to binder + if (allocatedAppWidgets) { + launchAppWidgetBinder(bindSources, bindTargets); + } + } + + /** + * Launch the widget binder that walks through the Launcher database, + * binding any matching widgets to the corresponding targets. We can't + * bind ourselves because our parent process can't obtain the + * BIND_APPWIDGET permission. + */ + private void launchAppWidgetBinder(int[] bindSources, ArrayList<ComponentName> bindTargets) { + final Intent intent = new Intent(); + intent.setComponent(new ComponentName("com.android.settings", + "com.android.settings.LauncherAppWidgetBinder")); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + + final Bundle extras = new Bundle(); + extras.putIntArray(EXTRA_BIND_SOURCES, bindSources); + extras.putParcelableArrayList(EXTRA_BIND_TARGETS, bindTargets); + intent.putExtras(extras); + + mContext.startActivity(intent); + } + + /** + * Loads the default set of favorite packages from an xml file. + * + * @param db The database to write the values into + */ + private int loadFavorites(SQLiteDatabase db) { + Intent intent = new Intent(Intent.ACTION_MAIN, null); + intent.addCategory(Intent.CATEGORY_LAUNCHER); + ContentValues values = new ContentValues(); + + PackageManager packageManager = mContext.getPackageManager(); + int i = 0; + try { + XmlResourceParser parser = mContext.getResources().getXml(R.xml.default_workspace); + AttributeSet attrs = Xml.asAttributeSet(parser); + XmlUtils.beginDocument(parser, TAG_FAVORITES); + + final int depth = parser.getDepth(); + + int type; + while (((type = parser.next()) != XmlPullParser.END_TAG || + parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) { + + if (type != XmlPullParser.START_TAG) { + continue; + } + + boolean added = false; + final String name = parser.getName(); + + TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Favorite); + + values.clear(); + values.put(LauncherSettings.Favorites.CONTAINER, + LauncherSettings.Favorites.CONTAINER_DESKTOP); + values.put(LauncherSettings.Favorites.SCREEN, + a.getString(R.styleable.Favorite_screen)); + values.put(LauncherSettings.Favorites.CELLX, + a.getString(R.styleable.Favorite_x)); + values.put(LauncherSettings.Favorites.CELLY, + a.getString(R.styleable.Favorite_y)); + + if (TAG_FAVORITE.equals(name)) { + added = addShortcut(db, values, a, packageManager, intent); + } else if (TAG_SEARCH.equals(name)) { + added = addSearchWidget(db, values); + } else if (TAG_CLOCK.equals(name)) { + added = addClockWidget(db, values); + } + + if (added) i++; + + a.recycle(); + } + } catch (XmlPullParserException e) { + Log.w(LOG_TAG, "Got exception parsing favorites.", e); + } catch (IOException e) { + Log.w(LOG_TAG, "Got exception parsing favorites.", e); + } + + return i; + } + + private boolean addShortcut(SQLiteDatabase db, ContentValues values, TypedArray a, + PackageManager packageManager, Intent intent) { + + ActivityInfo info; + String packageName = a.getString(R.styleable.Favorite_packageName); + String className = a.getString(R.styleable.Favorite_className); + try { + ComponentName cn = new ComponentName(packageName, className); + info = packageManager.getActivityInfo(cn, 0); + intent.setComponent(cn); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK + | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); + values.put(Favorites.INTENT, intent.toUri(0)); + values.put(Favorites.TITLE, info.loadLabel(packageManager).toString()); + values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPLICATION); + values.put(Favorites.SPANX, 1); + values.put(Favorites.SPANY, 1); + db.insert(TABLE_FAVORITES, null, values); + } catch (PackageManager.NameNotFoundException e) { + Log.w(LOG_TAG, "Unable to add favorite: " + packageName + + "/" + className, e); + return false; + } + return true; + } + + private boolean addSearchWidget(SQLiteDatabase db, ContentValues values) { + // Add a search box + values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_WIDGET_SEARCH); + values.put(Favorites.SPANX, 4); + values.put(Favorites.SPANY, 1); + db.insert(TABLE_FAVORITES, null, values); + + return true; + } + + private boolean addClockWidget(SQLiteDatabase db, ContentValues values) { + final int[] bindSources = new int[] { + Favorites.ITEM_TYPE_WIDGET_CLOCK, + }; + + final ArrayList<ComponentName> bindTargets = new ArrayList<ComponentName>(); + bindTargets.add(new ComponentName("com.android.alarmclock", + "com.android.alarmclock.AnalogAppWidgetProvider")); + + boolean allocatedAppWidgets = false; + + // Try binding to an analog clock widget + try { + int appWidgetId = mAppWidgetHost.allocateAppWidgetId(); + + values.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_WIDGET_CLOCK); + values.put(Favorites.SPANX, 2); + values.put(Favorites.SPANY, 2); + values.put(Favorites.APPWIDGET_ID, appWidgetId); + db.insert(TABLE_FAVORITES, null, values); + + allocatedAppWidgets = true; + } catch (RuntimeException ex) { + Log.e(LOG_TAG, "Problem allocating appWidgetId", ex); + } + + // If any appWidgetIds allocated, then launch over to binder + if (allocatedAppWidgets) { + launchAppWidgetBinder(bindSources, bindTargets); + } + + return allocatedAppWidgets; + } + } + + /** + * Build a query string that will match any row where the column matches + * anything in the values list. + */ + static String buildOrWhereString(String column, int[] values) { + StringBuilder selectWhere = new StringBuilder(); + for (int i = values.length - 1; i >= 0; i--) { + selectWhere.append(column).append("=").append(values[i]); + if (i > 0) { + selectWhere.append(" OR "); + } + } + return selectWhere.toString(); + } + + static class SqlArguments { + public final String table; + public final String where; + public final String[] args; + + SqlArguments(Uri url, String where, String[] args) { + if (url.getPathSegments().size() == 1) { + this.table = url.getPathSegments().get(0); + this.where = where; + this.args = args; + } else if (url.getPathSegments().size() != 2) { + throw new IllegalArgumentException("Invalid URI: " + url); + } else if (!TextUtils.isEmpty(where)) { + throw new UnsupportedOperationException("WHERE clause not supported: " + url); + } else { + this.table = url.getPathSegments().get(0); + this.where = "_id=" + ContentUris.parseId(url); + this.args = null; + } + } + + SqlArguments(Uri url) { + if (url.getPathSegments().size() == 1) { + table = url.getPathSegments().get(0); + where = null; + args = null; + } else { + throw new IllegalArgumentException("Invalid URI: " + url); + } + } + } +} diff --git a/src/com/android/launcher2/LauncherSettings.java b/src/com/android/launcher2/LauncherSettings.java new file mode 100644 index 0000000..8cc2559 --- /dev/null +++ b/src/com/android/launcher2/LauncherSettings.java @@ -0,0 +1,263 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.provider.BaseColumns; +import android.net.Uri; + +/** + * Settings related utilities. + */ +class LauncherSettings { + static interface BaseLauncherColumns extends BaseColumns { + /** + * Descriptive name of the gesture that can be displayed to the user. + * <P>Type: TEXT</P> + */ + static final String TITLE = "title"; + + /** + * The Intent URL of the gesture, describing what it points to. This + * value is given to {@link android.content.Intent#parseUri(String, int)} to create + * an Intent that can be launched. + * <P>Type: TEXT</P> + */ + static final String INTENT = "intent"; + + /** + * The type of the gesture + * + * <P>Type: INTEGER</P> + */ + static final String ITEM_TYPE = "itemType"; + + /** + * The gesture is an application + */ + static final int ITEM_TYPE_APPLICATION = 0; + + /** + * The gesture is an application created shortcut + */ + static final int ITEM_TYPE_SHORTCUT = 1; + + /** + * The icon type. + * <P>Type: INTEGER</P> + */ + static final String ICON_TYPE = "iconType"; + + /** + * The icon is a resource identified by a package name and an integer id. + */ + static final int ICON_TYPE_RESOURCE = 0; + + /** + * The icon is a bitmap. + */ + static final int ICON_TYPE_BITMAP = 1; + + /** + * The icon package name, if icon type is ICON_TYPE_RESOURCE. + * <P>Type: TEXT</P> + */ + static final String ICON_PACKAGE = "iconPackage"; + + /** + * The icon resource id, if icon type is ICON_TYPE_RESOURCE. + * <P>Type: TEXT</P> + */ + static final String ICON_RESOURCE = "iconResource"; + + /** + * The custom icon bitmap, if icon type is ICON_TYPE_BITMAP. + * <P>Type: BLOB</P> + */ + static final String ICON = "icon"; + } + + static final class Gestures implements BaseLauncherColumns { + /** + * The content:// style URL for this table + */ + static final Uri CONTENT_URI = Uri.parse("content://" + + LauncherProvider.AUTHORITY + "/" + LauncherProvider.TABLE_GESTURES + + "?" + LauncherProvider.PARAMETER_NOTIFY + "=true"); + + /** + * The content:// style URL for this table. When this Uri is used, no notification is + * sent if the content changes. + */ + static final Uri CONTENT_URI_NO_NOTIFICATION = Uri.parse("content://" + + LauncherProvider.AUTHORITY + "/" + LauncherProvider.TABLE_GESTURES + + "?" + LauncherProvider.PARAMETER_NOTIFY + "=false"); + + /** + * The content:// style URL for a given row, identified by its id. + * + * @param id The row id. + * @param notify True to send a notification is the content changes. + * + * @return The unique content URL for the specified row. + */ + static Uri getContentUri(long id, boolean notify) { + return Uri.parse("content://" + LauncherProvider.AUTHORITY + + "/" + LauncherProvider.TABLE_GESTURES + "/" + id + "?" + + LauncherProvider.PARAMETER_NOTIFY + "=" + notify); + } + } + + /** + * Favorites. When changing these values, be sure to update + * {@link com.android.settings.LauncherAppWidgetBinder} as needed. + */ + static final class Favorites implements BaseLauncherColumns { + /** + * The content:// style URL for this table + */ + static final Uri CONTENT_URI = Uri.parse("content://" + + LauncherProvider.AUTHORITY + "/" + LauncherProvider.TABLE_FAVORITES + + "?" + LauncherProvider.PARAMETER_NOTIFY + "=true"); + + /** + * The content:// style URL for this table. When this Uri is used, no notification is + * sent if the content changes. + */ + static final Uri CONTENT_URI_NO_NOTIFICATION = Uri.parse("content://" + + LauncherProvider.AUTHORITY + "/" + LauncherProvider.TABLE_FAVORITES + + "?" + LauncherProvider.PARAMETER_NOTIFY + "=false"); + + /** + * The content:// style URL for a given row, identified by its id. + * + * @param id The row id. + * @param notify True to send a notification is the content changes. + * + * @return The unique content URL for the specified row. + */ + static Uri getContentUri(long id, boolean notify) { + return Uri.parse("content://" + LauncherProvider.AUTHORITY + + "/" + LauncherProvider.TABLE_FAVORITES + "/" + id + "?" + + LauncherProvider.PARAMETER_NOTIFY + "=" + notify); + } + + /** + * The container holding the favorite + * <P>Type: INTEGER</P> + */ + static final String CONTAINER = "container"; + + /** + * The icon is a resource identified by a package name and an integer id. + */ + static final int CONTAINER_DESKTOP = -100; + + /** + * The screen holding the favorite (if container is CONTAINER_DESKTOP) + * <P>Type: INTEGER</P> + */ + static final String SCREEN = "screen"; + + /** + * The X coordinate of the cell holding the favorite + * (if container is CONTAINER_DESKTOP or CONTAINER_DOCK) + * <P>Type: INTEGER</P> + */ + static final String CELLX = "cellX"; + + /** + * The Y coordinate of the cell holding the favorite + * (if container is CONTAINER_DESKTOP) + * <P>Type: INTEGER</P> + */ + static final String CELLY = "cellY"; + + /** + * The X span of the cell holding the favorite + * <P>Type: INTEGER</P> + */ + static final String SPANX = "spanX"; + + /** + * The Y span of the cell holding the favorite + * <P>Type: INTEGER</P> + */ + static final String SPANY = "spanY"; + + /** + * The favorite is a user created folder + */ + static final int ITEM_TYPE_USER_FOLDER = 2; + + /** + * The favorite is a live folder + */ + static final int ITEM_TYPE_LIVE_FOLDER = 3; + + /** + * The favorite is a widget + */ + static final int ITEM_TYPE_APPWIDGET = 4; + + /** + * The favorite is a clock + */ + static final int ITEM_TYPE_WIDGET_CLOCK = 1000; + + /** + * The favorite is a search widget + */ + static final int ITEM_TYPE_WIDGET_SEARCH = 1001; + + /** + * The favorite is a photo frame + */ + static final int ITEM_TYPE_WIDGET_PHOTO_FRAME = 1002; + + /** + * The appWidgetId of the widget + * + * <P>Type: INTEGER</P> + */ + static final String APPWIDGET_ID = "appWidgetId"; + + /** + * Indicates whether this favorite is an application-created shortcut or not. + * If the value is 0, the favorite is not an application-created shortcut, if the + * value is 1, it is an application-created shortcut. + * <P>Type: INTEGER</P> + */ + @Deprecated + static final String IS_SHORTCUT = "isShortcut"; + + /** + * The URI associated with the favorite. It is used, for instance, by + * live folders to find the content provider. + * <P>Type: TEXT</P> + */ + static final String URI = "uri"; + + /** + * The display mode if the item is a live folder. + * <P>Type: INTEGER</P> + * + * @see android.provider.LiveFolders#DISPLAY_MODE_GRID + * @see android.provider.LiveFolders#DISPLAY_MODE_LIST + */ + static final String DISPLAY_MODE = "displayMode"; + } +} diff --git a/src/com/android/launcher2/LiveFolder.java b/src/com/android/launcher2/LiveFolder.java new file mode 100644 index 0000000..44a283e --- /dev/null +++ b/src/com/android/launcher2/LiveFolder.java @@ -0,0 +1,134 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.content.Context; +import android.content.Intent; +import android.util.AttributeSet; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.AdapterView; +import android.net.Uri; +import android.provider.LiveFolders; +import android.os.AsyncTask; +import android.database.Cursor; + +import java.lang.ref.WeakReference; + +public class LiveFolder extends Folder { + private AsyncTask<LiveFolderInfo,Void,Cursor> mLoadingTask; + + public LiveFolder(Context context, AttributeSet attrs) { + super(context, attrs); + } + + static LiveFolder fromXml(Context context, FolderInfo folderInfo) { + final int layout = isDisplayModeList(folderInfo) ? + R.layout.live_folder_list : R.layout.live_folder_grid; + return (LiveFolder) LayoutInflater.from(context).inflate(layout, null); + } + + private static boolean isDisplayModeList(FolderInfo folderInfo) { + return ((LiveFolderInfo) folderInfo).displayMode == + LiveFolders.DISPLAY_MODE_LIST; + } + + @Override + public void onItemClick(AdapterView parent, View v, int position, long id) { + LiveFolderAdapter.ViewHolder holder = (LiveFolderAdapter.ViewHolder) v.getTag(); + + if (holder.useBaseIntent) { + final Intent baseIntent = ((LiveFolderInfo) mInfo).baseIntent; + if (baseIntent != null) { + final Intent intent = new Intent(baseIntent); + Uri uri = baseIntent.getData(); + uri = uri.buildUpon().appendPath(Long.toString(holder.id)).build(); + intent.setData(uri); + mLauncher.startActivitySafely(intent); + } + } else if (holder.intent != null) { + mLauncher.startActivitySafely(holder.intent); + } + } + + @Override + public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { + return false; + } + + void bind(FolderInfo info) { + super.bind(info); + if (mLoadingTask != null && mLoadingTask.getStatus() == AsyncTask.Status.RUNNING) { + mLoadingTask.cancel(true); + } + mLoadingTask = new FolderLoadingTask(this).execute((LiveFolderInfo) info); + } + + @Override + void onOpen() { + super.onOpen(); + requestFocus(); + } + + @Override + void onClose() { + super.onClose(); + if (mLoadingTask != null && mLoadingTask.getStatus() == AsyncTask.Status.RUNNING) { + mLoadingTask.cancel(true); + } + + // The adapter can be null if onClose() is called before FolderLoadingTask + // is done querying the provider + final LiveFolderAdapter adapter = (LiveFolderAdapter) mContent.getAdapter(); + if (adapter != null) { + adapter.cleanup(); + } + } + + static class FolderLoadingTask extends AsyncTask<LiveFolderInfo, Void, Cursor> { + private final WeakReference<LiveFolder> mFolder; + private LiveFolderInfo mInfo; + + FolderLoadingTask(LiveFolder folder) { + mFolder = new WeakReference<LiveFolder>(folder); + } + + protected Cursor doInBackground(LiveFolderInfo... params) { + final LiveFolder folder = mFolder.get(); + if (folder != null) { + mInfo = params[0]; + return LiveFolderAdapter.query(folder.mLauncher, mInfo); + } + return null; + } + + @Override + protected void onPostExecute(Cursor cursor) { + if (!isCancelled()) { + if (cursor != null) { + final LiveFolder folder = mFolder.get(); + if (folder != null) { + final Launcher launcher = folder.mLauncher; + folder.setContentAdapter(new LiveFolderAdapter(launcher, mInfo, cursor)); + } + } + } else if (cursor != null) { + cursor.close(); + } + } + } +} diff --git a/src/com/android/launcher2/LiveFolderAdapter.java b/src/com/android/launcher2/LiveFolderAdapter.java new file mode 100644 index 0000000..c405041 --- /dev/null +++ b/src/com/android/launcher2/LiveFolderAdapter.java @@ -0,0 +1,209 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.widget.CursorAdapter; +import android.widget.TextView; +import android.widget.ImageView; +import android.content.Context; +import android.content.Intent; +import android.content.res.Resources; +import android.content.pm.PackageManager; +import android.view.View; +import android.view.ViewGroup; +import android.view.LayoutInflater; +import android.database.Cursor; +import android.provider.LiveFolders; +import android.graphics.drawable.Drawable; +import android.graphics.BitmapFactory; +import android.graphics.Bitmap; + +import java.net.URISyntaxException; +import java.util.HashMap; +import java.lang.ref.SoftReference; + +class LiveFolderAdapter extends CursorAdapter { + private boolean mIsList; + private LayoutInflater mInflater; + + private final HashMap<String, Drawable> mIcons = new HashMap<String, Drawable>(); + private final HashMap<Long, SoftReference<Drawable>> mCustomIcons = + new HashMap<Long, SoftReference<Drawable>>(); + private final Launcher mLauncher; + + LiveFolderAdapter(Launcher launcher, LiveFolderInfo info, Cursor cursor) { + super(launcher, cursor, true); + mIsList = info.displayMode == LiveFolders.DISPLAY_MODE_LIST; + mInflater = LayoutInflater.from(launcher); + mLauncher = launcher; + + mLauncher.startManagingCursor(getCursor()); + } + + static Cursor query(Context context, LiveFolderInfo info) { + return context.getContentResolver().query(info.uri, null, null, + null, LiveFolders.NAME + " ASC"); + } + + public View newView(Context context, Cursor cursor, ViewGroup parent) { + View view; + final ViewHolder holder = new ViewHolder(); + + if (!mIsList) { + view = mInflater.inflate(R.layout.application_boxed, parent, false); + } else { + view = mInflater.inflate(R.layout.application_list, parent, false); + holder.description = (TextView) view.findViewById(R.id.description); + holder.icon = (ImageView) view.findViewById(R.id.icon); + } + + holder.name = (TextView) view.findViewById(R.id.name); + + holder.idIndex = cursor.getColumnIndexOrThrow(LiveFolders._ID); + holder.nameIndex = cursor.getColumnIndexOrThrow(LiveFolders.NAME); + holder.descriptionIndex = cursor.getColumnIndex(LiveFolders.DESCRIPTION); + holder.intentIndex = cursor.getColumnIndex(LiveFolders.INTENT); + holder.iconBitmapIndex = cursor.getColumnIndex(LiveFolders.ICON_BITMAP); + holder.iconResourceIndex = cursor.getColumnIndex(LiveFolders.ICON_RESOURCE); + holder.iconPackageIndex = cursor.getColumnIndex(LiveFolders.ICON_PACKAGE); + + view.setTag(holder); + + return view; + } + + public void bindView(View view, Context context, Cursor cursor) { + final ViewHolder holder = (ViewHolder) view.getTag(); + + holder.id = cursor.getLong(holder.idIndex); + final Drawable icon = loadIcon(context, cursor, holder); + + holder.name.setText(cursor.getString(holder.nameIndex)); + + if (!mIsList) { + holder.name.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null); + } else { + final boolean hasIcon = icon != null; + holder.icon.setVisibility(hasIcon ? View.VISIBLE : View.GONE); + if (hasIcon) holder.icon.setImageDrawable(icon); + + if (holder.descriptionIndex != -1) { + final String description = cursor.getString(holder.descriptionIndex); + if (description != null) { + holder.description.setText(description); + holder.description.setVisibility(View.VISIBLE); + } else { + holder.description.setVisibility(View.GONE); + } + } else { + holder.description.setVisibility(View.GONE); + } + } + + if (holder.intentIndex != -1) { + try { + holder.intent = Intent.parseUri(cursor.getString(holder.intentIndex), 0); + } catch (URISyntaxException e) { + // Ignore + } + } else { + holder.useBaseIntent = true; + } + } + + private Drawable loadIcon(Context context, Cursor cursor, ViewHolder holder) { + Drawable icon = null; + byte[] data = null; + + if (holder.iconBitmapIndex != -1) { + data = cursor.getBlob(holder.iconBitmapIndex); + } + + if (data != null) { + final SoftReference<Drawable> reference = mCustomIcons.get(holder.id); + if (reference != null) { + icon = reference.get(); + } + + if (icon == null) { + final Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); + icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, mContext)); + mCustomIcons.put(holder.id, new SoftReference<Drawable>(icon)); + } + } else if (holder.iconResourceIndex != -1 && holder.iconPackageIndex != -1) { + final String resource = cursor.getString(holder.iconResourceIndex); + icon = mIcons.get(resource); + if (icon == null) { + try { + final PackageManager packageManager = context.getPackageManager(); + Resources resources = packageManager.getResourcesForApplication( + cursor.getString(holder.iconPackageIndex)); + final int id = resources.getIdentifier(resource, + null, null); + icon = Utilities.createIconThumbnail(resources.getDrawable(id), mContext); + mIcons.put(resource, icon); + } catch (Exception e) { + // Ignore + } + } + } + + return icon; + } + + void cleanup() { + for (Drawable icon : mIcons.values()) { + icon.setCallback(null); + } + mIcons.clear(); + + for (SoftReference<Drawable> icon : mCustomIcons.values()) { + final Drawable drawable = icon.get(); + if (drawable != null) { + drawable.setCallback(null); + } + } + mCustomIcons.clear(); + + final Cursor cursor = getCursor(); + if (cursor != null) { + try { + cursor.close(); + } finally { + mLauncher.stopManagingCursor(cursor); + } + } + } + + static class ViewHolder { + TextView name; + TextView description; + ImageView icon; + + Intent intent; + long id; + boolean useBaseIntent; + + int idIndex; + int nameIndex; + int descriptionIndex = -1; + int intentIndex = -1; + int iconBitmapIndex = -1; + int iconResourceIndex = -1; + int iconPackageIndex = -1; + } +} diff --git a/src/com/android/launcher2/LiveFolderIcon.java b/src/com/android/launcher2/LiveFolderIcon.java new file mode 100644 index 0000000..14a4ee6 --- /dev/null +++ b/src/com/android/launcher2/LiveFolderIcon.java @@ -0,0 +1,76 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.content.Context; +import android.content.res.Resources; +import android.util.AttributeSet; +import android.view.ViewGroup; +import android.view.LayoutInflater; +import android.graphics.drawable.Drawable; + +public class LiveFolderIcon extends FolderIcon { + public LiveFolderIcon(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public LiveFolderIcon(Context context) { + super(context); + } + + static LiveFolderIcon fromXml(int resId, Launcher launcher, ViewGroup group, + LiveFolderInfo folderInfo) { + + LiveFolderIcon icon = (LiveFolderIcon) + LayoutInflater.from(launcher).inflate(resId, group, false); + + final Resources resources = launcher.getResources(); + Drawable d = folderInfo.icon; + if (d == null) { + d = Utilities.createIconThumbnail( + resources.getDrawable(R.drawable.ic_launcher_folder), launcher); + folderInfo.filtered = true; + } + icon.setCompoundDrawablesWithIntrinsicBounds(null, d, null, null); + icon.setText(folderInfo.title); + icon.setTag(folderInfo); + icon.setOnClickListener(launcher); + + return icon; + } + + @Override + public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo) { + return false; + } + + @Override + public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo) { + } + + @Override + public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo) { + } + + @Override + public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo) { + } + + @Override + public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo) { + } +} diff --git a/src/com/android/launcher2/LiveFolderInfo.java b/src/com/android/launcher2/LiveFolderInfo.java new file mode 100644 index 0000000..ec865a4 --- /dev/null +++ b/src/com/android/launcher2/LiveFolderInfo.java @@ -0,0 +1,75 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.content.ContentValues; +import android.content.Intent; +import android.graphics.drawable.Drawable; +import android.net.Uri; + +class LiveFolderInfo extends FolderInfo { + + /** + * The base intent, if it exists. + */ + Intent baseIntent; + + /** + * The live folder's content uri. + */ + Uri uri; + + /** + * The live folder's display type. + */ + int displayMode; + + /** + * The live folder icon. + */ + Drawable icon; + + /** + * When set to true, indicates that the icon has been resized. + */ + boolean filtered; + + /** + * Reference to the live folder icon as an application's resource. + */ + Intent.ShortcutIconResource iconResource; + + LiveFolderInfo() { + itemType = LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER; + } + + @Override + void onAddToDatabase(ContentValues values) { + super.onAddToDatabase(values); + values.put(LauncherSettings.Favorites.TITLE, title.toString()); + values.put(LauncherSettings.Favorites.URI, uri.toString()); + if (baseIntent != null) { + values.put(LauncherSettings.Favorites.INTENT, baseIntent.toUri(0)); + } + values.put(LauncherSettings.Favorites.ICON_TYPE, LauncherSettings.Favorites.ICON_TYPE_RESOURCE); + values.put(LauncherSettings.Favorites.DISPLAY_MODE, displayMode); + if (iconResource != null) { + values.put(LauncherSettings.Favorites.ICON_PACKAGE, iconResource.packageName); + values.put(LauncherSettings.Favorites.ICON_RESOURCE, iconResource.resourceName); + } + } +} diff --git a/src/com/android/launcher2/Search.java b/src/com/android/launcher2/Search.java new file mode 100644 index 0000000..8a7c352 --- /dev/null +++ b/src/com/android/launcher2/Search.java @@ -0,0 +1,407 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.app.SearchManager; +import android.content.ActivityNotFoundException; +import android.content.BroadcastReceiver; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.content.res.Configuration; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.server.search.SearchableInfo; +import android.server.search.Searchables; +import android.util.AttributeSet; +import android.util.Log; +import android.view.KeyEvent; +import android.view.View; +import android.view.View.OnClickListener; +import android.view.View.OnKeyListener; +import android.view.View.OnLongClickListener; +import android.view.animation.AccelerateDecelerateInterpolator; +import android.view.animation.Animation; +import android.view.animation.Interpolator; +import android.view.animation.Transformation; +import android.view.inputmethod.InputMethodManager; +import android.widget.ImageButton; +import android.widget.LinearLayout; +import android.widget.TextView; + +public class Search extends LinearLayout + implements OnClickListener, OnKeyListener, OnLongClickListener { + + // Speed at which the widget slides up/down, in pixels/ms. + private static final float ANIMATION_VELOCITY = 1.0f; + + private final String TAG = "SearchWidget"; + + private Launcher mLauncher; + + private TextView mSearchText; + private ImageButton mVoiceButton; + + /** The animation that morphs the search widget to the search dialog. */ + private Animation mMorphAnimation; + + /** The animation that morphs the search widget back to its normal position. */ + private Animation mUnmorphAnimation; + + // These four are passed to Launcher.startSearch() when the search widget + // has finished morphing. They are instance variables to make it possible to update + // them while the widget is morphing. + private String mInitialQuery; + private boolean mSelectInitialQuery; + private Bundle mAppSearchData; + private boolean mGlobalSearch; + + // For voice searching + private Intent mVoiceSearchIntent; + + private Drawable mGooglePlaceholder; + + private SearchManager mSearchManager; + + /** + * Used to inflate the Workspace from XML. + * + * @param context The application's context. + * @param attrs The attributes set containing the Workspace's customization values. + */ + public Search(Context context, AttributeSet attrs) { + super(context, attrs); + + Interpolator interpolator = new AccelerateDecelerateInterpolator(); + + mMorphAnimation = new ToParentOriginAnimation(); + // no need to apply transformation before the animation starts, + // since the gadget is already in its normal place. + mMorphAnimation.setFillBefore(false); + // stay in the top position after the animation finishes + mMorphAnimation.setFillAfter(true); + mMorphAnimation.setInterpolator(interpolator); + mMorphAnimation.setAnimationListener(new Animation.AnimationListener() { + // The amount of time before the animation ends to show the search dialog. + private static final long TIME_BEFORE_ANIMATION_END = 80; + + // The runnable which we'll pass to our handler to show the search dialog. + private final Runnable mShowSearchDialogRunnable = new Runnable() { + public void run() { + showSearchDialog(); + } + }; + + public void onAnimationEnd(Animation animation) { } + public void onAnimationRepeat(Animation animation) { } + public void onAnimationStart(Animation animation) { + // Make the search dialog show up ideally *just* as the animation reaches + // the top, to aid the illusion that the widget becomes the search dialog. + // Otherwise, there is a short delay when the widget reaches the top before + // the search dialog shows. We do this roughly 80ms before the animation ends. + getHandler().postDelayed( + mShowSearchDialogRunnable, + Math.max(mMorphAnimation.getDuration() - TIME_BEFORE_ANIMATION_END, 0)); + } + }); + + mUnmorphAnimation = new FromParentOriginAnimation(); + // stay in the top position until the animation starts + mUnmorphAnimation.setFillBefore(true); + // no need to apply transformation after the animation finishes, + // since the gadget is now back in its normal place. + mUnmorphAnimation.setFillAfter(false); + mUnmorphAnimation.setInterpolator(interpolator); + mUnmorphAnimation.setAnimationListener(new Animation.AnimationListener(){ + public void onAnimationEnd(Animation animation) { + clearAnimation(); + } + public void onAnimationRepeat(Animation animation) { } + public void onAnimationStart(Animation animation) { } + }); + + mVoiceSearchIntent = new Intent(android.speech.RecognizerIntent.ACTION_WEB_SEARCH); + mVoiceSearchIntent.putExtra(android.speech.RecognizerIntent.EXTRA_LANGUAGE_MODEL, + android.speech.RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH); + + mSearchManager = (SearchManager) getContext().getSystemService(Context.SEARCH_SERVICE); + } + + /** + * Implements OnClickListener. + */ + public void onClick(View v) { + if (v == mVoiceButton) { + startVoiceSearch(); + } else { + mLauncher.onSearchRequested(); + } + } + + private void startVoiceSearch() { + try { + getContext().startActivity(mVoiceSearchIntent); + } catch (ActivityNotFoundException ex) { + // Should not happen, since we check the availability of + // voice search before showing the button. But just in case... + Log.w(TAG, "Could not find voice search activity"); + } + } + + /** + * Sets the query text. The query field is not editable, instead we forward + * the key events to the launcher, which keeps track of the text, + * calls setQuery() to show it, and gives it to the search dialog. + */ + public void setQuery(String query) { + mSearchText.setText(query, TextView.BufferType.NORMAL); + } + + /** + * Morph the search gadget to the search dialog. + * See {@link Activity.startSearch()} for the arguments. + */ + public void startSearch(String initialQuery, boolean selectInitialQuery, + Bundle appSearchData, boolean globalSearch) { + mInitialQuery = initialQuery; + mSelectInitialQuery = selectInitialQuery; + mAppSearchData = appSearchData; + mGlobalSearch = globalSearch; + + if (isAtTop()) { + showSearchDialog(); + } else { + // Call up the keyboard before we actually call the search dialog so that it + // (hopefully) animates in at about the same time as the widget animation, and + // so that it becomes available as soon as possible. Only do this if a hard + // keyboard is not currently available. + if (getContext().getResources().getConfiguration().hardKeyboardHidden == + Configuration.HARDKEYBOARDHIDDEN_YES) { + InputMethodManager inputManager = (InputMethodManager) + getContext().getSystemService(Context.INPUT_METHOD_SERVICE); + inputManager.showSoftInputUnchecked(0, null); + } + + // Start the animation, unless it has already started. + if (getAnimation() != mMorphAnimation) { + mMorphAnimation.setDuration(getAnimationDuration()); + startAnimation(mMorphAnimation); + } + } + } + + /** + * Shows the system search dialog immediately, without any animation. + */ + private void showSearchDialog() { + mLauncher.showSearchDialog( + mInitialQuery, mSelectInitialQuery, mAppSearchData, mGlobalSearch); + } + + /** + * Restore the search gadget to its normal position. + * + * @param animate Whether to animate the movement of the gadget. + */ + public void stopSearch(boolean animate) { + setQuery(""); + + // Only restore if we are not already restored. + if (getAnimation() == mMorphAnimation) { + if (animate && !isAtTop()) { + mUnmorphAnimation.setDuration(getAnimationDuration()); + startAnimation(mUnmorphAnimation); + } else { + clearAnimation(); + } + } + } + + private boolean isAtTop() { + return getTop() == 0; + } + + private int getAnimationDuration() { + return (int) (getTop() / ANIMATION_VELOCITY); + } + + /** + * Modify clearAnimation() to invalidate the parent. This works around + * an issue where the region where the end of the animation placed the view + * was not redrawn after clearing the animation. + */ + @Override + public void clearAnimation() { + Animation animation = getAnimation(); + if (animation != null) { + super.clearAnimation(); + if (animation.hasEnded() + && animation.getFillAfter() + && animation.willChangeBounds()) { + ((View) getParent()).invalidate(); + } else { + invalidate(); + } + } + } + + public boolean onKey(View v, int keyCode, KeyEvent event) { + if (!event.isSystem() && + (keyCode != KeyEvent.KEYCODE_DPAD_UP) && + (keyCode != KeyEvent.KEYCODE_DPAD_DOWN) && + (keyCode != KeyEvent.KEYCODE_DPAD_LEFT) && + (keyCode != KeyEvent.KEYCODE_DPAD_RIGHT) && + (keyCode != KeyEvent.KEYCODE_DPAD_CENTER)) { + // Forward key events to Launcher, which will forward text + // to search dialog + switch (event.getAction()) { + case KeyEvent.ACTION_DOWN: + return mLauncher.onKeyDown(keyCode, event); + case KeyEvent.ACTION_MULTIPLE: + return mLauncher.onKeyMultiple(keyCode, event.getRepeatCount(), event); + case KeyEvent.ACTION_UP: + return mLauncher.onKeyUp(keyCode, event); + } + } + return false; + } + + /** + * Implements OnLongClickListener to pass long clicks on child views + * to the widget. This makes it possible to pick up the widget by long + * clicking on the text field or a button. + */ + public boolean onLongClick(View v) { + return performLongClick(); + } + + @Override + protected void onFinishInflate() { + super.onFinishInflate(); + + mSearchText = (TextView) findViewById(R.id.search_src_text); + mVoiceButton = (ImageButton) findViewById(R.id.search_voice_btn); + + mGooglePlaceholder = getContext().getResources().getDrawable(R.drawable.placeholder_google); + mContext.registerReceiver(mBroadcastReceiver, + new IntentFilter(SearchManager.INTENT_ACTION_SEARCH_SETTINGS_CHANGED)); + + mSearchText.setOnKeyListener(this); + + mSearchText.setOnClickListener(this); + mVoiceButton.setOnClickListener(this); + setOnClickListener(this); + + mSearchText.setOnLongClickListener(this); + mVoiceButton.setOnLongClickListener(this); + + configureVoiceSearchButton(); + setUpTextField(); + } + + @Override + public void onDetachedFromWindow() { + super.onDetachedFromWindow(); + if (mBroadcastReceiver != null) getContext().unregisterReceiver(mBroadcastReceiver); + } + + /** + * If appropriate & available, configure voice search + * + * Note: Because the home screen search widget is always web search, we only check for + * getVoiceSearchLaunchWebSearch() modes. We don't support the alternate form of app-specific + * voice search. + */ + private void configureVoiceSearchButton() { + // Enable the voice search button if there is an activity that can handle it + PackageManager pm = getContext().getPackageManager(); + ResolveInfo ri = pm.resolveActivity(mVoiceSearchIntent, + PackageManager.MATCH_DEFAULT_ONLY); + boolean voiceSearchVisible = ri != null; + + // finally, set visible state of voice search button, as appropriate + mVoiceButton.setVisibility(voiceSearchVisible ? View.VISIBLE : View.GONE); + } + + /** + * Sets up the look of the text field. If Google is the chosen search provider, includes + * a Google logo as placeholder. + */ + private void setUpTextField() { + boolean showGooglePlaceholder = false; + SearchableInfo webSearchSearchable = mSearchManager.getDefaultSearchableForWebSearch(); + if (webSearchSearchable != null) { + ComponentName webSearchComponent = webSearchSearchable.getSearchActivity(); + if (webSearchComponent != null) { + String componentString = webSearchComponent.flattenToShortString(); + if (Searchables.ENHANCED_GOOGLE_SEARCH_COMPONENT_NAME.equals(componentString) || + Searchables.GOOGLE_SEARCH_COMPONENT_NAME.equals(componentString)) { + showGooglePlaceholder = true; + } + } + } + + mSearchText.setCompoundDrawablesWithIntrinsicBounds( + showGooglePlaceholder ? mGooglePlaceholder : null, null, null, null); + } + + /** + * Sets the {@link Launcher} that this gadget will call on to display the search dialog. + */ + public void setLauncher(Launcher launcher) { + mLauncher = launcher; + } + + // Broadcast receiver for web search provider change notifications + private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + if (SearchManager.INTENT_ACTION_SEARCH_SETTINGS_CHANGED.equals(action)) { + setUpTextField(); + } + } + }; + + /** + * Moves the view to the top left corner of its parent. + */ + private class ToParentOriginAnimation extends Animation { + @Override + protected void applyTransformation(float interpolatedTime, Transformation t) { + float dx = -getLeft() * interpolatedTime; + float dy = -getTop() * interpolatedTime; + t.getMatrix().setTranslate(dx, dy); + } + } + + /** + * Moves the view from the top left corner of its parent. + */ + private class FromParentOriginAnimation extends Animation { + @Override + protected void applyTransformation(float interpolatedTime, Transformation t) { + float dx = -getLeft() * (1.0f - interpolatedTime); + float dy = -getTop() * (1.0f - interpolatedTime); + t.getMatrix().setTranslate(dx, dy); + } + } + +} diff --git a/src/com/android/launcher2/UninstallShortcutReceiver.java b/src/com/android/launcher2/UninstallShortcutReceiver.java new file mode 100644 index 0000000..9fd6298 --- /dev/null +++ b/src/com/android/launcher2/UninstallShortcutReceiver.java @@ -0,0 +1,80 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.ContentResolver; +import android.database.Cursor; +import android.net.Uri; +import android.widget.Toast; + +import java.net.URISyntaxException; + +public class UninstallShortcutReceiver extends BroadcastReceiver { + private static final String ACTION_UNINSTALL_SHORTCUT = + "com.android.launcher.action.UNINSTALL_SHORTCUT"; + + public void onReceive(Context context, Intent data) { + if (!ACTION_UNINSTALL_SHORTCUT.equals(data.getAction())) { + return; + } + + Intent intent = data.getParcelableExtra(Intent.EXTRA_SHORTCUT_INTENT); + String name = data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME); + boolean duplicate = data.getBooleanExtra(Launcher.EXTRA_SHORTCUT_DUPLICATE, true); + + if (intent != null && name != null) { + final ContentResolver cr = context.getContentResolver(); + Cursor c = cr.query(LauncherSettings.Favorites.CONTENT_URI, + new String[] { LauncherSettings.Favorites._ID, LauncherSettings.Favorites.INTENT }, + LauncherSettings.Favorites.TITLE + "=?", new String[] { name }, null); + + final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT); + final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID); + + boolean changed = false; + + try { + while (c.moveToNext()) { + try { + if (intent.filterEquals(Intent.parseUri(c.getString(intentIndex), 0))) { + final long id = c.getLong(idIndex); + final Uri uri = LauncherSettings.Favorites.getContentUri(id, false); + cr.delete(uri, null, null); + changed = true; + if (!duplicate) { + break; + } + } + } catch (URISyntaxException e) { + // Ignore + } + } + } finally { + c.close(); + } + + if (changed) { + cr.notifyChange(LauncherSettings.Favorites.CONTENT_URI, null); + Toast.makeText(context, context.getString(R.string.shortcut_uninstalled, name), + Toast.LENGTH_SHORT).show(); + } + } + } +} diff --git a/src/com/android/launcher2/UserFolder.java b/src/com/android/launcher2/UserFolder.java new file mode 100644 index 0000000..6cdfed9 --- /dev/null +++ b/src/com/android/launcher2/UserFolder.java @@ -0,0 +1,80 @@ +package com.android.launcher; + +import android.content.Context; +import android.graphics.Rect; +import android.util.AttributeSet; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.ArrayAdapter; + +/** + * Folder which contains applications or shortcuts chosen by the user. + * + */ +public class UserFolder extends Folder implements DropTarget { + public UserFolder(Context context, AttributeSet attrs) { + super(context, attrs); + } + + /** + * Creates a new UserFolder, inflated from R.layout.user_folder. + * + * @param context The application's context. + * + * @return A new UserFolder. + */ + static UserFolder fromXml(Context context) { + return (UserFolder) LayoutInflater.from(context).inflate(R.layout.user_folder, null); + } + + public boolean acceptDrop(DragSource source, int x, int y, int xOffset, int yOffset, + Object dragInfo) { + final ItemInfo item = (ItemInfo) dragInfo; + final int itemType = item.itemType; + return (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION || + itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) && item.container != mInfo.id; + } + + public Rect estimateDropLocation(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo, Rect recycle) { + return null; + } + + public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo) { + final ApplicationInfo item = (ApplicationInfo) dragInfo; + //noinspection unchecked + ((ArrayAdapter<ApplicationInfo>) mContent.getAdapter()).add((ApplicationInfo) dragInfo); + LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, 0, 0); + } + + public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo) { + } + + public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo) { + } + + public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo) { + } + + @Override + public void onDropCompleted(View target, boolean success) { + if (success) { + //noinspection unchecked + ArrayAdapter<ApplicationInfo> adapter = + (ArrayAdapter<ApplicationInfo>) mContent.getAdapter(); + adapter.remove(mDragItem); + } + } + + void bind(FolderInfo info) { + super.bind(info); + setContentAdapter(new ApplicationsAdapter(mContext, ((UserFolderInfo) info).contents)); + } + + // When the folder opens, we need to refresh the GridView's selection by + // forcing a layout + @Override + void onOpen() { + super.onOpen(); + requestFocus(); + } +} diff --git a/src/com/android/launcher2/UserFolderInfo.java b/src/com/android/launcher2/UserFolderInfo.java new file mode 100644 index 0000000..639894e --- /dev/null +++ b/src/com/android/launcher2/UserFolderInfo.java @@ -0,0 +1,59 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.content.ContentValues; + +import java.util.ArrayList; + +/** + * Represents a folder containing shortcuts or apps. + */ +class UserFolderInfo extends FolderInfo { + /** + * The apps and shortcuts + */ + ArrayList<ApplicationInfo> contents = new ArrayList<ApplicationInfo>(); + + UserFolderInfo() { + itemType = LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER; + } + + /** + * Add an app or shortcut + * + * @param item + */ + public void add(ApplicationInfo item) { + contents.add(item); + } + + /** + * Remove an app or shortcut + * + * @param item + */ + public void remove(ApplicationInfo item) { + contents.remove(item); + } + + @Override + void onAddToDatabase(ContentValues values) { + super.onAddToDatabase(values); + values.put(LauncherSettings.Favorites.TITLE, title.toString()); + } +} diff --git a/src/com/android/launcher2/Utilities.java b/src/com/android/launcher2/Utilities.java new file mode 100644 index 0000000..4a22cba --- /dev/null +++ b/src/com/android/launcher2/Utilities.java @@ -0,0 +1,204 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; +import android.graphics.drawable.PaintDrawable; +import android.graphics.Bitmap; +import android.graphics.PixelFormat; +import android.graphics.Canvas; +import android.graphics.PaintFlagsDrawFilter; +import android.graphics.Paint; +import android.graphics.Rect; +import android.util.DisplayMetrics; +import android.content.res.Resources; +import android.content.Context; + +/** + * Various utilities shared amongst the Launcher's classes. + */ +final class Utilities { + private static int sIconWidth = -1; + private static int sIconHeight = -1; + + private static final Paint sPaint = new Paint(); + private static final Rect sBounds = new Rect(); + private static final Rect sOldBounds = new Rect(); + private static Canvas sCanvas = new Canvas(); + + static { + sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, + Paint.FILTER_BITMAP_FLAG)); + } + + static Bitmap centerToFit(Bitmap bitmap, int width, int height, Context context) { + final int bitmapWidth = bitmap.getWidth(); + final int bitmapHeight = bitmap.getHeight(); + + if (bitmapWidth < width || bitmapHeight < height) { + int color = context.getResources().getColor(R.color.window_background); + + Bitmap centered = Bitmap.createBitmap(bitmapWidth < width ? width : bitmapWidth, + bitmapHeight < height ? height : bitmapHeight, Bitmap.Config.RGB_565); + centered.setDensity(bitmap.getDensity()); + Canvas canvas = new Canvas(centered); + canvas.drawColor(color); + canvas.drawBitmap(bitmap, (width - bitmapWidth) / 2.0f, (height - bitmapHeight) / 2.0f, + null); + + bitmap = centered; + } + + return bitmap; + } + + /** + * Returns a Drawable representing the thumbnail of the specified Drawable. + * The size of the thumbnail is defined by the dimension + * android.R.dimen.launcher_application_icon_size. + * + * This method is not thread-safe and should be invoked on the UI thread only. + * + * @param icon The icon to get a thumbnail of. + * @param context The application's context. + * + * @return A thumbnail for the specified icon or the icon itself if the + * thumbnail could not be created. + */ + static Drawable createIconThumbnail(Drawable icon, Context context) { + if (sIconWidth == -1) { + final Resources resources = context.getResources(); + sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size); + } + + int width = sIconWidth; + int height = sIconHeight; + + float scale = 1.0f; + if (icon instanceof PaintDrawable) { + PaintDrawable painter = (PaintDrawable) icon; + painter.setIntrinsicWidth(width); + painter.setIntrinsicHeight(height); + } else if (icon instanceof BitmapDrawable) { + // Ensure the bitmap has a density. + BitmapDrawable bitmapDrawable = (BitmapDrawable) icon; + Bitmap bitmap = bitmapDrawable.getBitmap(); + if (bitmap.getDensity() == Bitmap.DENSITY_NONE) { + bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics()); + } + } + int iconWidth = icon.getIntrinsicWidth(); + int iconHeight = icon.getIntrinsicHeight(); + + if (width > 0 && height > 0) { + if (width < iconWidth || height < iconHeight || scale != 1.0f) { + final float ratio = (float) iconWidth / iconHeight; + + if (iconWidth > iconHeight) { + height = (int) (width / ratio); + } else if (iconHeight > iconWidth) { + width = (int) (height * ratio); + } + + final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ? + Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; + final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c); + final Canvas canvas = sCanvas; + canvas.setBitmap(thumb); + // Copy the old bounds to restore them later + // If we were to do oldBounds = icon.getBounds(), + // the call to setBounds() that follows would + // change the same instance and we would lose the + // old bounds + sOldBounds.set(icon.getBounds()); + final int x = (sIconWidth - width) / 2; + final int y = (sIconHeight - height) / 2; + icon.setBounds(x, y, x + width, y + height); + icon.draw(canvas); + icon.setBounds(sOldBounds); + icon = new FastBitmapDrawable(thumb); + } else if (iconWidth < width && iconHeight < height) { + final Bitmap.Config c = Bitmap.Config.ARGB_8888; + final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c); + final Canvas canvas = sCanvas; + canvas.setBitmap(thumb); + sOldBounds.set(icon.getBounds()); + final int x = (width - iconWidth) / 2; + final int y = (height - iconHeight) / 2; + icon.setBounds(x, y, x + iconWidth, y + iconHeight); + icon.draw(canvas); + icon.setBounds(sOldBounds); + icon = new FastBitmapDrawable(thumb); + } + } + + return icon; + } + + /** + * Returns a Bitmap representing the thumbnail of the specified Bitmap. + * The size of the thumbnail is defined by the dimension + * android.R.dimen.launcher_application_icon_size. + * + * This method is not thread-safe and should be invoked on the UI thread only. + * + * @param bitmap The bitmap to get a thumbnail of. + * @param context The application's context. + * + * @return A thumbnail for the specified bitmap or the bitmap itself if the + * thumbnail could not be created. + */ + static Bitmap createBitmapThumbnail(Bitmap bitmap, Context context) { + if (sIconWidth == -1) { + final Resources resources = context.getResources(); + sIconWidth = sIconHeight = (int) resources.getDimension( + android.R.dimen.app_icon_size); + } + + int width = sIconWidth; + int height = sIconHeight; + + final int bitmapWidth = bitmap.getWidth(); + final int bitmapHeight = bitmap.getHeight(); + + if (width > 0 && height > 0 && (width < bitmapWidth || height < bitmapHeight)) { + final float ratio = (float) bitmapWidth / bitmapHeight; + + if (bitmapWidth > bitmapHeight) { + height = (int) (width / ratio); + } else if (bitmapHeight > bitmapWidth) { + width = (int) (height * ratio); + } + + final Bitmap.Config c = (width == sIconWidth && height == sIconHeight) ? + bitmap.getConfig() : Bitmap.Config.ARGB_8888; + final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c); + final Canvas canvas = sCanvas; + final Paint paint = sPaint; + canvas.setBitmap(thumb); + paint.setDither(false); + paint.setFilterBitmap(true); + sBounds.set((sIconWidth - width) / 2, (sIconHeight - height) / 2, width, height); + sOldBounds.set(0, 0, bitmapWidth, bitmapHeight); + canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint); + return thumb; + } + + return bitmap; + } +} diff --git a/src/com/android/launcher2/WallpaperChooser.java b/src/com/android/launcher2/WallpaperChooser.java new file mode 100644 index 0000000..70396d0 --- /dev/null +++ b/src/com/android/launcher2/WallpaperChooser.java @@ -0,0 +1,264 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.app.Activity; +import android.app.IWallpaperService; +import android.content.res.Resources; +import android.graphics.BitmapFactory; +import android.graphics.Bitmap; +import android.graphics.drawable.Drawable; +import android.os.Bundle; +import android.os.ParcelFileDescriptor; +import android.os.RemoteException; +import android.os.ServiceManager; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.Window; +import android.view.View.OnClickListener; +import android.widget.AdapterView; +import android.widget.BaseAdapter; +import android.widget.Button; +import android.widget.Gallery; +import android.widget.ImageView; + +import java.io.IOException; +import java.io.InputStream; +import java.io.FileOutputStream; +import java.util.ArrayList; +import java.util.Collections; + +public class WallpaperChooser extends Activity implements AdapterView.OnItemSelectedListener, + OnClickListener { + + private static final Integer[] THUMB_IDS = { + R.drawable.wallpaper_lake_small, + R.drawable.wallpaper_sunset_small, + R.drawable.wallpaper_beach_small, + R.drawable.wallpaper_snow_leopard_small, + R.drawable.wallpaper_path_small, + R.drawable.wallpaper_sunrise_small, + R.drawable.wallpaper_mountain_small, + R.drawable.wallpaper_road_small, + R.drawable.wallpaper_jellyfish_small, + R.drawable.wallpaper_zanzibar_small, + R.drawable.wallpaper_blue_small, + R.drawable.wallpaper_grey_small, + R.drawable.wallpaper_green_small, + R.drawable.wallpaper_pink_small, + }; + + private static final Integer[] IMAGE_IDS = { + R.drawable.wallpaper_lake, + R.drawable.wallpaper_sunset, + R.drawable.wallpaper_beach, + R.drawable.wallpaper_snow_leopard, + R.drawable.wallpaper_path, + R.drawable.wallpaper_sunrise, + R.drawable.wallpaper_mountain, + R.drawable.wallpaper_road, + R.drawable.wallpaper_jellyfish, + R.drawable.wallpaper_zanzibar, + R.drawable.wallpaper_blue, + R.drawable.wallpaper_grey, + R.drawable.wallpaper_green, + R.drawable.wallpaper_pink, + }; + + private Gallery mGallery; + private ImageView mImageView; + private boolean mIsWallpaperSet; + + private BitmapFactory.Options mOptions; + private Bitmap mBitmap; + + private ArrayList<Integer> mThumbs; + private ArrayList<Integer> mImages; + private ArrayList<String> mNames; + + @Override + public void onCreate(Bundle icicle) { + super.onCreate(icicle); + requestWindowFeature(Window.FEATURE_NO_TITLE); + + findWallpapers(); + + setContentView(R.layout.wallpaper_chooser); + + mOptions = new BitmapFactory.Options(); + mOptions.inDither = false; + mOptions.inPreferredConfig = Bitmap.Config.ARGB_8888; + + mGallery = (Gallery) findViewById(R.id.gallery); + mGallery.setAdapter(new ImageAdapter(this)); + mGallery.setOnItemSelectedListener(this); + mGallery.setCallbackDuringFling(false); + + Button b = (Button) findViewById(R.id.set); + b.setOnClickListener(this); + + mImageView = (ImageView) findViewById(R.id.wallpaper); + } + + private void findWallpapers() { + mThumbs = new ArrayList<Integer>(THUMB_IDS.length + 4); + Collections.addAll(mThumbs, THUMB_IDS); + + mImages = new ArrayList<Integer>(IMAGE_IDS.length + 4); + Collections.addAll(mImages, IMAGE_IDS); + + final Resources resources = getResources(); + + mNames = new ArrayList<String>(IMAGE_IDS.length + 4); + for (int res: mImages) { + mNames.add("res:" + resources.getResourceName(res)); + } + + final String[] extras = resources.getStringArray(R.array.extra_wallpapers); + final String packageName = getApplication().getPackageName(); + + for (String extra : extras) { + int res = resources.getIdentifier(extra, "drawable", packageName); + if (res != 0) { + final int thumbRes = resources.getIdentifier(extra + "_small", + "drawable", packageName); + + if (thumbRes != 0) { + mThumbs.add(thumbRes); + mImages.add(res); + mNames.add("res:" + extra); + } + } + } + } + + @Override + protected void onResume() { + super.onResume(); + mIsWallpaperSet = false; + } + + public void onItemSelected(AdapterView parent, View v, int position, long id) { + final ImageView view = mImageView; + Bitmap b = BitmapFactory.decodeResource(getResources(), mImages.get(position), mOptions); + view.setImageBitmap(b); + + // Help the GC + if (mBitmap != null) { + mBitmap.recycle(); + } + mBitmap = b; + + final Drawable drawable = view.getDrawable(); + drawable.setFilterBitmap(true); + drawable.setDither(true); + } + + /* + * When using touch if you tap an image it triggers both the onItemClick and + * the onTouchEvent causing the wallpaper to be set twice. Ensure we only + * set the wallpaper once. + */ + private void selectWallpaper(int position) { + if (mIsWallpaperSet) { + return; + } + + mIsWallpaperSet = true; + try { + InputStream stream = getResources().openRawResource(mImages.get(position)); + setWallpaper(stream, mNames.get(position)); + setResult(RESULT_OK); + finish(); + } catch (IOException e) { + Log.e(Launcher.LOG_TAG, "Failed to set wallpaper: " + e); + } + } + + public void onNothingSelected(AdapterView parent) { + } + + private class ImageAdapter extends BaseAdapter { + private LayoutInflater mLayoutInflater; + + ImageAdapter(WallpaperChooser context) { + mLayoutInflater = context.getLayoutInflater(); + } + + public int getCount() { + return mThumbs.size(); + } + + public Object getItem(int position) { + return position; + } + + public long getItemId(int position) { + return position; + } + + public View getView(int position, View convertView, ViewGroup parent) { + ImageView image; + + if (convertView == null) { + image = (ImageView) mLayoutInflater.inflate(R.layout.wallpaper_item, parent, false); + } else { + image = (ImageView) convertView; + } + + image.setImageResource(mThumbs.get(position)); + image.getDrawable().setDither(true); + return image; + } + } + + public void onClick(View v) { + selectWallpaper(mGallery.getSelectedItemPosition()); + } + + private void setWallpaper(InputStream data, String name) throws IOException { + try { + IWallpaperService svc = IWallpaperService.Stub.asInterface( + ServiceManager.getService(WALLPAPER_SERVICE)); + ParcelFileDescriptor fd = svc.setWallpaper(name); + if (fd == null) { + return; + } + FileOutputStream fos = null; + try { + fos = new ParcelFileDescriptor.AutoCloseOutputStream(fd); + setWallpaper(data, fos); + } finally { + if (fos != null) { + fos.close(); + } + } + } catch (RemoteException e) { + } + } + + private void setWallpaper(InputStream data, FileOutputStream fos) + throws IOException { + byte[] buffer = new byte[32768]; + int amt; + while ((amt=data.read(buffer)) > 0) { + fos.write(buffer, 0, amt); + } + } +} diff --git a/src/com/android/launcher2/Widget.java b/src/com/android/launcher2/Widget.java new file mode 100644 index 0000000..4f246cc --- /dev/null +++ b/src/com/android/launcher2/Widget.java @@ -0,0 +1,36 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.content.ContentValues; +import android.graphics.Bitmap; + +/** + * Represents one instance of a Launcher widget, such as search. + */ +class Widget extends ItemInfo { + int layoutResource; + + static Widget makeSearch() { + Widget w = new Widget(); + w.itemType = LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH; + w.spanX = 4; + w.spanY = 1; + w.layoutResource = R.layout.widget_search; + return w; + } +} diff --git a/src/com/android/launcher2/Workspace.java b/src/com/android/launcher2/Workspace.java new file mode 100644 index 0000000..f5dc633 --- /dev/null +++ b/src/com/android/launcher2/Workspace.java @@ -0,0 +1,1361 @@ +/* + * 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. + */ + +package com.android.launcher; + +import android.content.Context; +import android.content.Intent; +import android.content.ComponentName; +import android.content.res.TypedArray; +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.RectF; +import android.graphics.Rect; +import android.graphics.Region; +import android.graphics.drawable.Drawable; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.view.VelocityTracker; +import android.view.View; +import android.view.ViewConfiguration; +import android.view.ViewGroup; +import android.view.ViewParent; +import android.widget.Scroller; +import android.widget.TextView; +import android.os.Parcelable; +import android.os.Parcel; + +import java.util.ArrayList; + +/** + * The workspace is a wide area with a wallpaper and a finite number of screens. Each + * screen contains a number of icons, folders or widgets the user can interact with. + * A workspace is meant to be used with a fixed width only. + */ +public class Workspace extends ViewGroup implements DropTarget, DragSource, DragScroller { + private static final int INVALID_SCREEN = -1; + + /** + * The velocity at which a fling gesture will cause us to snap to the next screen + */ + private static final int SNAP_VELOCITY = 1000; + + private int mDefaultScreen; + + private Paint mPaint; + private Bitmap mWallpaper; + + private int mWallpaperWidth; + private int mWallpaperHeight; + private float mWallpaperOffset; + private boolean mWallpaperLoaded; + + private boolean mFirstLayout = true; + + private int mCurrentScreen; + private int mNextScreen = INVALID_SCREEN; + private Scroller mScroller; + private VelocityTracker mVelocityTracker; + + /** + * CellInfo for the cell that is currently being dragged + */ + private CellLayout.CellInfo mDragInfo; + + /** + * Target drop area calculated during last acceptDrop call. + */ + private int[] mTargetCell = null; + + private float mLastMotionX; + private float mLastMotionY; + + private final static int TOUCH_STATE_REST = 0; + private final static int TOUCH_STATE_SCROLLING = 1; + + private int mTouchState = TOUCH_STATE_REST; + + private OnLongClickListener mLongClickListener; + + private Launcher mLauncher; + private DragController mDragger; + + /** + * Cache of vacant cells, used during drag events and invalidated as needed. + */ + private CellLayout.CellInfo mVacantCache = null; + + private int[] mTempCell = new int[2]; + private int[] mTempEstimate = new int[2]; + + private boolean mAllowLongPress; + private boolean mLocked; + + private int mTouchSlop; + private int mMaximumVelocity; + + final Rect mDrawerBounds = new Rect(); + final Rect mClipBounds = new Rect(); + int mDrawerContentHeight; + int mDrawerContentWidth; + + /** + * Used to inflate the Workspace from XML. + * + * @param context The application's context. + * @param attrs The attribtues set containing the Workspace's customization values. + */ + public Workspace(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + /** + * Used to inflate the Workspace from XML. + * + * @param context The application's context. + * @param attrs The attribtues set containing the Workspace's customization values. + * @param defStyle Unused. + */ + public Workspace(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Workspace, defStyle, 0); + mDefaultScreen = a.getInt(R.styleable.Workspace_defaultScreen, 1); + a.recycle(); + + initWorkspace(); + } + + /** + * Initializes various states for this workspace. + */ + private void initWorkspace() { + mScroller = new Scroller(getContext()); + mCurrentScreen = mDefaultScreen; + Launcher.setScreen(mCurrentScreen); + + mPaint = new Paint(); + mPaint.setDither(false); + + final ViewConfiguration configuration = ViewConfiguration.get(getContext()); + mTouchSlop = configuration.getScaledTouchSlop(); + mMaximumVelocity = configuration.getScaledMaximumFlingVelocity(); + } + + /** + * Set the background's wallpaper. + */ + void loadWallpaper(Bitmap bitmap) { + mWallpaper = bitmap; + mWallpaperLoaded = true; + requestLayout(); + invalidate(); + } + + @Override + public void addView(View child, int index, LayoutParams params) { + if (!(child instanceof CellLayout)) { + throw new IllegalArgumentException("A Workspace can only have CellLayout children."); + } + super.addView(child, index, params); + } + + @Override + public void addView(View child) { + if (!(child instanceof CellLayout)) { + throw new IllegalArgumentException("A Workspace can only have CellLayout children."); + } + super.addView(child); + } + + @Override + public void addView(View child, int index) { + if (!(child instanceof CellLayout)) { + throw new IllegalArgumentException("A Workspace can only have CellLayout children."); + } + super.addView(child, index); + } + + @Override + public void addView(View child, int width, int height) { + if (!(child instanceof CellLayout)) { + throw new IllegalArgumentException("A Workspace can only have CellLayout children."); + } + super.addView(child, width, height); + } + + @Override + public void addView(View child, LayoutParams params) { + if (!(child instanceof CellLayout)) { + throw new IllegalArgumentException("A Workspace can only have CellLayout children."); + } + super.addView(child, params); + } + + /** + * @return The open folder on the current screen, or null if there is none + */ + Folder getOpenFolder() { + CellLayout currentScreen = (CellLayout) getChildAt(mCurrentScreen); + int count = currentScreen.getChildCount(); + for (int i = 0; i < count; i++) { + View child = currentScreen.getChildAt(i); + CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); + if (lp.cellHSpan == 4 && lp.cellVSpan == 4 && child instanceof Folder) { + return (Folder) child; + } + } + return null; + } + + ArrayList<Folder> getOpenFolders() { + final int screens = getChildCount(); + ArrayList<Folder> folders = new ArrayList<Folder>(screens); + + for (int screen = 0; screen < screens; screen++) { + CellLayout currentScreen = (CellLayout) getChildAt(screen); + int count = currentScreen.getChildCount(); + for (int i = 0; i < count; i++) { + View child = currentScreen.getChildAt(i); + CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); + if (lp.cellHSpan == 4 && lp.cellVSpan == 4 && child instanceof Folder) { + folders.add((Folder) child); + break; + } + } + } + + return folders; + } + + boolean isDefaultScreenShowing() { + return mCurrentScreen == mDefaultScreen; + } + + /** + * Returns the index of the currently displayed screen. + * + * @return The index of the currently displayed screen. + */ + int getCurrentScreen() { + return mCurrentScreen; + } + + /** + * Computes a bounding rectangle for a range of cells + * + * @param cellX X coordinate of upper left corner expressed as a cell position + * @param cellY Y coordinate of upper left corner expressed as a cell position + * @param cellHSpan Width in cells + * @param cellVSpan Height in cells + * @param rect Rectnagle into which to put the results + */ + public void cellToRect(int cellX, int cellY, int cellHSpan, int cellVSpan, RectF rect) { + ((CellLayout)getChildAt(mCurrentScreen)).cellToRect(cellX, cellY, + cellHSpan, cellVSpan, rect); + } + + /** + * Sets the current screen. + * + * @param currentScreen + */ + void setCurrentScreen(int currentScreen) { + clearVacantCache(); + mCurrentScreen = Math.max(0, Math.min(currentScreen, getChildCount() - 1)); + scrollTo(mCurrentScreen * getWidth(), 0); + invalidate(); + } + + /** + * Shows the default screen (defined by the firstScreen attribute in XML.) + */ + void showDefaultScreen() { + setCurrentScreen(mDefaultScreen); + } + + /** + * Adds the specified child in the current screen. The position and dimension of + * the child are defined by x, y, spanX and spanY. + * + * @param child The child to add in one of the workspace's screens. + * @param x The X position of the child in the screen's grid. + * @param y The Y position of the child in the screen's grid. + * @param spanX The number of cells spanned horizontally by the child. + * @param spanY The number of cells spanned vertically by the child. + */ + void addInCurrentScreen(View child, int x, int y, int spanX, int spanY) { + addInScreen(child, mCurrentScreen, x, y, spanX, spanY, false); + } + + /** + * Adds the specified child in the current screen. The position and dimension of + * the child are defined by x, y, spanX and spanY. + * + * @param child The child to add in one of the workspace's screens. + * @param x The X position of the child in the screen's grid. + * @param y The Y position of the child in the screen's grid. + * @param spanX The number of cells spanned horizontally by the child. + * @param spanY The number of cells spanned vertically by the child. + * @param insert When true, the child is inserted at the beginning of the children list. + */ + void addInCurrentScreen(View child, int x, int y, int spanX, int spanY, boolean insert) { + addInScreen(child, mCurrentScreen, x, y, spanX, spanY, insert); + } + + /** + * Adds the specified child in the specified screen. The position and dimension of + * the child are defined by x, y, spanX and spanY. + * + * @param child The child to add in one of the workspace's screens. + * @param screen The screen in which to add the child. + * @param x The X position of the child in the screen's grid. + * @param y The Y position of the child in the screen's grid. + * @param spanX The number of cells spanned horizontally by the child. + * @param spanY The number of cells spanned vertically by the child. + */ + void addInScreen(View child, int screen, int x, int y, int spanX, int spanY) { + addInScreen(child, screen, x, y, spanX, spanY, false); + } + + /** + * Adds the specified child in the specified screen. The position and dimension of + * the child are defined by x, y, spanX and spanY. + * + * @param child The child to add in one of the workspace's screens. + * @param screen The screen in which to add the child. + * @param x The X position of the child in the screen's grid. + * @param y The Y position of the child in the screen's grid. + * @param spanX The number of cells spanned horizontally by the child. + * @param spanY The number of cells spanned vertically by the child. + * @param insert When true, the child is inserted at the beginning of the children list. + */ + void addInScreen(View child, int screen, int x, int y, int spanX, int spanY, boolean insert) { + if (screen < 0 || screen >= getChildCount()) { + throw new IllegalStateException("The screen must be >= 0 and < " + getChildCount()); + } + + clearVacantCache(); + + final CellLayout group = (CellLayout) getChildAt(screen); + CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); + if (lp == null) { + lp = new CellLayout.LayoutParams(x, y, spanX, spanY); + } else { + lp.cellX = x; + lp.cellY = y; + lp.cellHSpan = spanX; + lp.cellVSpan = spanY; + } + group.addView(child, insert ? 0 : -1, lp); + if (!(child instanceof Folder)) { + child.setOnLongClickListener(mLongClickListener); + } + } + + void addWidget(View view, Widget widget) { + addInScreen(view, widget.screen, widget.cellX, widget.cellY, widget.spanX, + widget.spanY, false); + } + + void addWidget(View view, Widget widget, boolean insert) { + addInScreen(view, widget.screen, widget.cellX, widget.cellY, widget.spanX, + widget.spanY, insert); + } + + CellLayout.CellInfo findAllVacantCells(boolean[] occupied) { + CellLayout group = (CellLayout) getChildAt(mCurrentScreen); + if (group != null) { + return group.findAllVacantCells(occupied, null); + } + return null; + } + + private void clearVacantCache() { + if (mVacantCache != null) { + mVacantCache.clearVacantCells(); + mVacantCache = null; + } + } + + /** + * Returns the coordinate of a vacant cell for the current screen. + */ + boolean getVacantCell(int[] vacant, int spanX, int spanY) { + CellLayout group = (CellLayout) getChildAt(mCurrentScreen); + if (group != null) { + return group.getVacantCell(vacant, spanX, spanY); + } + return false; + } + + /** + * Adds the specified child in the current screen. The position and dimension of + * the child are defined by x, y, spanX and spanY. + * + * @param child The child to add in one of the workspace's screens. + * @param spanX The number of cells spanned horizontally by the child. + * @param spanY The number of cells spanned vertically by the child. + */ + void fitInCurrentScreen(View child, int spanX, int spanY) { + fitInScreen(child, mCurrentScreen, spanX, spanY); + } + + /** + * Adds the specified child in the specified screen. The position and dimension of + * the child are defined by x, y, spanX and spanY. + * + * @param child The child to add in one of the workspace's screens. + * @param screen The screen in which to add the child. + * @param spanX The number of cells spanned horizontally by the child. + * @param spanY The number of cells spanned vertically by the child. + */ + void fitInScreen(View child, int screen, int spanX, int spanY) { + if (screen < 0 || screen >= getChildCount()) { + throw new IllegalStateException("The screen must be >= 0 and < " + getChildCount()); + } + + final CellLayout group = (CellLayout) getChildAt(screen); + boolean vacant = group.getVacantCell(mTempCell, spanX, spanY); + if (vacant) { + group.addView(child, + new CellLayout.LayoutParams(mTempCell[0], mTempCell[1], spanX, spanY)); + child.setOnLongClickListener(mLongClickListener); + if (!(child instanceof Folder)) { + child.setOnLongClickListener(mLongClickListener); + } + } + } + + /** + * Registers the specified listener on each screen contained in this workspace. + * + * @param l The listener used to respond to long clicks. + */ + @Override + public void setOnLongClickListener(OnLongClickListener l) { + mLongClickListener = l; + final int count = getChildCount(); + for (int i = 0; i < count; i++) { + getChildAt(i).setOnLongClickListener(l); + } + } + + @Override + public void computeScroll() { + if (mScroller.computeScrollOffset()) { + mScrollX = mScroller.getCurrX(); + mScrollY = mScroller.getCurrY(); + postInvalidate(); + } else if (mNextScreen != INVALID_SCREEN) { + mCurrentScreen = Math.max(0, Math.min(mNextScreen, getChildCount() - 1)); + Launcher.setScreen(mCurrentScreen); + mNextScreen = INVALID_SCREEN; + clearChildrenCache(); + } + } + + @Override + public boolean isOpaque() { + return !mWallpaper.hasAlpha(); + } + + @Override + protected void dispatchDraw(Canvas canvas) { + boolean restore = false; + + // If the all apps drawer is open and the drawing region for the workspace + // is contained within the drawer's bounds, we skip the drawing. This requires + // the drawer to be fully opaque. + if (mLauncher.isDrawerUp()) { + final Rect clipBounds = mClipBounds; + canvas.getClipBounds(clipBounds); + clipBounds.offset(-mScrollX, -mScrollY); + if (mDrawerBounds.contains(clipBounds)) { + return; + } + } else if (mLauncher.isDrawerMoving()) { + restore = true; + canvas.save(Canvas.CLIP_SAVE_FLAG); + + final View view = mLauncher.getDrawerHandle(); + final int top = view.getTop() + view.getHeight(); + + canvas.clipRect(mScrollX, top, mScrollX + mDrawerContentWidth, + top + mDrawerContentHeight, Region.Op.DIFFERENCE); + } + + float x = mScrollX * mWallpaperOffset; + if (x + mWallpaperWidth < mRight - mLeft) { + x = mRight - mLeft - mWallpaperWidth; + } + + canvas.drawBitmap(mWallpaper, x, (mBottom - mTop - mWallpaperHeight) / 2, mPaint); + + // ViewGroup.dispatchDraw() supports many features we don't need: + // clip to padding, layout animation, animation listener, disappearing + // children, etc. The following implementation attempts to fast-track + // the drawing dispatch by drawing only what we know needs to be drawn. + + boolean fastDraw = mTouchState != TOUCH_STATE_SCROLLING && mNextScreen == INVALID_SCREEN; + // If we are not scrolling or flinging, draw only the current screen + if (fastDraw) { + drawChild(canvas, getChildAt(mCurrentScreen), getDrawingTime()); + } else { + final long drawingTime = getDrawingTime(); + // If we are flinging, draw only the current screen and the target screen + if (mNextScreen >= 0 && mNextScreen < getChildCount() && + Math.abs(mCurrentScreen - mNextScreen) == 1) { + drawChild(canvas, getChildAt(mCurrentScreen), drawingTime); + drawChild(canvas, getChildAt(mNextScreen), drawingTime); + } else { + // If we are scrolling, draw all of our children + final int count = getChildCount(); + for (int i = 0; i < count; i++) { + drawChild(canvas, getChildAt(i), drawingTime); + } + } + } + + if (restore) { + canvas.restore(); + } + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + + final int width = MeasureSpec.getSize(widthMeasureSpec); + final int widthMode = MeasureSpec.getMode(widthMeasureSpec); + if (widthMode != MeasureSpec.EXACTLY) { + throw new IllegalStateException("Workspace can only be used in EXACTLY mode."); + } + + final int heightMode = MeasureSpec.getMode(heightMeasureSpec); + if (heightMode != MeasureSpec.EXACTLY) { + throw new IllegalStateException("Workspace can only be used in EXACTLY mode."); + } + + // The children are given the same width and height as the workspace + final int count = getChildCount(); + for (int i = 0; i < count; i++) { + getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec); + } + + if (mWallpaperLoaded) { + mWallpaperLoaded = false; + mWallpaper = Utilities.centerToFit(mWallpaper, width, + MeasureSpec.getSize(heightMeasureSpec), getContext()); + mWallpaperWidth = mWallpaper.getWidth(); + mWallpaperHeight = mWallpaper.getHeight(); + } + + final int wallpaperWidth = mWallpaperWidth; + mWallpaperOffset = wallpaperWidth > width ? (count * width - wallpaperWidth) / + ((count - 1) * (float) width) : 1.0f; + + if (mFirstLayout) { + scrollTo(mCurrentScreen * width, 0); + mFirstLayout = false; + } + } + + @Override + protected void onLayout(boolean changed, int left, int top, int right, int bottom) { + int childLeft = 0; + + final int count = getChildCount(); + for (int i = 0; i < count; i++) { + final View child = getChildAt(i); + if (child.getVisibility() != View.GONE) { + final int childWidth = child.getMeasuredWidth(); + child.layout(childLeft, 0, childLeft + childWidth, child.getMeasuredHeight()); + childLeft += childWidth; + } + } + } + + @Override + public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { + int screen = indexOfChild(child); + if (screen != mCurrentScreen || !mScroller.isFinished()) { + if (!mLauncher.isWorkspaceLocked()) { + snapToScreen(screen); + } + return true; + } + return false; + } + + @Override + protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { + if (mLauncher.isDrawerDown()) { + final Folder openFolder = getOpenFolder(); + if (openFolder != null) { + return openFolder.requestFocus(direction, previouslyFocusedRect); + } else { + int focusableScreen; + if (mNextScreen != INVALID_SCREEN) { + focusableScreen = mNextScreen; + } else { + focusableScreen = mCurrentScreen; + } + getChildAt(focusableScreen).requestFocus(direction, previouslyFocusedRect); + } + } + return false; + } + + @Override + public boolean dispatchUnhandledMove(View focused, int direction) { + if (direction == View.FOCUS_LEFT) { + if (getCurrentScreen() > 0) { + snapToScreen(getCurrentScreen() - 1); + return true; + } + } else if (direction == View.FOCUS_RIGHT) { + if (getCurrentScreen() < getChildCount() - 1) { + snapToScreen(getCurrentScreen() + 1); + return true; + } + } + return super.dispatchUnhandledMove(focused, direction); + } + + @Override + public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { + if (mLauncher.isDrawerDown()) { + final Folder openFolder = getOpenFolder(); + if (openFolder == null) { + getChildAt(mCurrentScreen).addFocusables(views, direction); + if (direction == View.FOCUS_LEFT) { + if (mCurrentScreen > 0) { + getChildAt(mCurrentScreen - 1).addFocusables(views, direction); + } + } else if (direction == View.FOCUS_RIGHT){ + if (mCurrentScreen < getChildCount() - 1) { + getChildAt(mCurrentScreen + 1).addFocusables(views, direction); + } + } + } else { + openFolder.addFocusables(views, direction); + } + } + } + + @Override + public boolean onInterceptTouchEvent(MotionEvent ev) { + if (mLocked || !mLauncher.isDrawerDown()) { + return true; + } + + /* + * This method JUST determines whether we want to intercept the motion. + * If we return true, onTouchEvent will be called and we do the actual + * scrolling there. + */ + + /* + * Shortcut the most recurring case: the user is in the dragging + * state and he is moving his finger. We want to intercept this + * motion. + */ + final int action = ev.getAction(); + if ((action == MotionEvent.ACTION_MOVE) && (mTouchState != TOUCH_STATE_REST)) { + return true; + } + + final float x = ev.getX(); + final float y = ev.getY(); + + switch (action) { + case MotionEvent.ACTION_MOVE: + /* + * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check + * whether the user has moved far enough from his original down touch. + */ + + /* + * Locally do absolute value. mLastMotionX is set to the y value + * of the down event. + */ + final int xDiff = (int) Math.abs(x - mLastMotionX); + final int yDiff = (int) Math.abs(y - mLastMotionY); + + final int touchSlop = mTouchSlop; + boolean xMoved = xDiff > touchSlop; + boolean yMoved = yDiff > touchSlop; + + if (xMoved || yMoved) { + + if (xMoved) { + // Scroll if the user moved far enough along the X axis + mTouchState = TOUCH_STATE_SCROLLING; + enableChildrenCache(); + } + // Either way, cancel any pending longpress + if (mAllowLongPress) { + mAllowLongPress = false; + // Try canceling the long press. It could also have been scheduled + // by a distant descendant, so use the mAllowLongPress flag to block + // everything + final View currentScreen = getChildAt(mCurrentScreen); + currentScreen.cancelLongPress(); + } + } + break; + + case MotionEvent.ACTION_DOWN: + // Remember location of down touch + mLastMotionX = x; + mLastMotionY = y; + mAllowLongPress = true; + + /* + * If being flinged and user touches the screen, initiate drag; + * otherwise don't. mScroller.isFinished should be false when + * being flinged. + */ + mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING; + break; + + case MotionEvent.ACTION_CANCEL: + case MotionEvent.ACTION_UP: + // Release the drag + clearChildrenCache(); + mTouchState = TOUCH_STATE_REST; + mAllowLongPress = false; + break; + } + + /* + * The only time we want to intercept motion events is if we are in the + * drag mode. + */ + return mTouchState != TOUCH_STATE_REST; + } + + void enableChildrenCache() { + final int count = getChildCount(); + for (int i = 0; i < count; i++) { + final CellLayout layout = (CellLayout) getChildAt(i); + layout.setChildrenDrawnWithCacheEnabled(true); + layout.setChildrenDrawingCacheEnabled(true); + } + } + + void clearChildrenCache() { + final int count = getChildCount(); + for (int i = 0; i < count; i++) { + final CellLayout layout = (CellLayout) getChildAt(i); + layout.setChildrenDrawnWithCacheEnabled(false); + } + } + + @Override + public boolean onTouchEvent(MotionEvent ev) { + if (mLocked || !mLauncher.isDrawerDown()) { + return true; + } + + if (mVelocityTracker == null) { + mVelocityTracker = VelocityTracker.obtain(); + } + mVelocityTracker.addMovement(ev); + + final int action = ev.getAction(); + final float x = ev.getX(); + + switch (action) { + case MotionEvent.ACTION_DOWN: + /* + * If being flinged and user touches, stop the fling. isFinished + * will be false if being flinged. + */ + if (!mScroller.isFinished()) { + mScroller.abortAnimation(); + } + + // Remember where the motion event started + mLastMotionX = x; + break; + case MotionEvent.ACTION_MOVE: + if (mTouchState == TOUCH_STATE_SCROLLING) { + // Scroll to follow the motion event + final int deltaX = (int) (mLastMotionX - x); + mLastMotionX = x; + + if (deltaX < 0) { + if (mScrollX > 0) { + scrollBy(Math.max(-mScrollX, deltaX), 0); + } + } else if (deltaX > 0) { + final int availableToScroll = getChildAt(getChildCount() - 1).getRight() - + mScrollX - getWidth(); + if (availableToScroll > 0) { + scrollBy(Math.min(availableToScroll, deltaX), 0); + } + } + } + break; + case MotionEvent.ACTION_UP: + if (mTouchState == TOUCH_STATE_SCROLLING) { + final VelocityTracker velocityTracker = mVelocityTracker; + velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); + int velocityX = (int) velocityTracker.getXVelocity(); + + if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) { + // Fling hard enough to move left + snapToScreen(mCurrentScreen - 1); + } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < getChildCount() - 1) { + // Fling hard enough to move right + snapToScreen(mCurrentScreen + 1); + } else { + snapToDestination(); + } + + if (mVelocityTracker != null) { + mVelocityTracker.recycle(); + mVelocityTracker = null; + } + } + mTouchState = TOUCH_STATE_REST; + break; + case MotionEvent.ACTION_CANCEL: + mTouchState = TOUCH_STATE_REST; + } + + return true; + } + + private void snapToDestination() { + final int screenWidth = getWidth(); + final int whichScreen = (mScrollX + (screenWidth / 2)) / screenWidth; + + snapToScreen(whichScreen); + } + + void snapToScreen(int whichScreen) { + if (!mScroller.isFinished()) return; + + clearVacantCache(); + enableChildrenCache(); + + whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1)); + boolean changingScreens = whichScreen != mCurrentScreen; + + mNextScreen = whichScreen; + + View focusedChild = getFocusedChild(); + if (focusedChild != null && changingScreens && focusedChild == getChildAt(mCurrentScreen)) { + focusedChild.clearFocus(); + } + + final int newX = whichScreen * getWidth(); + final int delta = newX - mScrollX; + mScroller.startScroll(mScrollX, 0, delta, 0, Math.abs(delta) * 2); + invalidate(); + } + + void startDrag(CellLayout.CellInfo cellInfo) { + View child = cellInfo.cell; + + // Make sure the drag was started by a long press as opposed to a long click. + // Note that Search takes focus when clicked rather than entering touch mode + if (!child.isInTouchMode() && !(child instanceof Search)) { + return; + } + + mDragInfo = cellInfo; + mDragInfo.screen = mCurrentScreen; + + CellLayout current = ((CellLayout) getChildAt(mCurrentScreen)); + + current.onDragChild(child); + mDragger.startDrag(child, this, child.getTag(), DragController.DRAG_ACTION_MOVE); + invalidate(); + } + + @Override + protected Parcelable onSaveInstanceState() { + final SavedState state = new SavedState(super.onSaveInstanceState()); + state.currentScreen = mCurrentScreen; + return state; + } + + @Override + protected void onRestoreInstanceState(Parcelable state) { + SavedState savedState = (SavedState) state; + super.onRestoreInstanceState(savedState.getSuperState()); + if (savedState.currentScreen != -1) { + mCurrentScreen = savedState.currentScreen; + Launcher.setScreen(mCurrentScreen); + } + } + + void addApplicationShortcut(ApplicationInfo info, CellLayout.CellInfo cellInfo) { + addApplicationShortcut(info, cellInfo, false); + } + + void addApplicationShortcut(ApplicationInfo info, CellLayout.CellInfo cellInfo, + boolean insertAtFirst) { + final CellLayout layout = (CellLayout) getChildAt(cellInfo.screen); + final int[] result = new int[2]; + + layout.cellToPoint(cellInfo.cellX, cellInfo.cellY, result); + onDropExternal(result[0], result[1], info, layout, insertAtFirst); + } + + public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset, Object dragInfo) { + final CellLayout cellLayout = getCurrentDropLayout(); + if (source != this) { + onDropExternal(x - xOffset, y - yOffset, dragInfo, cellLayout); + } else { + // Move internally + if (mDragInfo != null) { + final View cell = mDragInfo.cell; + if (mCurrentScreen != mDragInfo.screen) { + final CellLayout originalCellLayout = (CellLayout) getChildAt(mDragInfo.screen); + originalCellLayout.removeView(cell); + cellLayout.addView(cell); + } + mTargetCell = estimateDropCell(x - xOffset, y - yOffset, + mDragInfo.spanX, mDragInfo.spanY, cell, cellLayout, mTargetCell); + cellLayout.onDropChild(cell, mTargetCell); + + final ItemInfo info = (ItemInfo)cell.getTag(); + CellLayout.LayoutParams lp = (CellLayout.LayoutParams) cell.getLayoutParams(); + LauncherModel.moveItemInDatabase(mLauncher, info, + LauncherSettings.Favorites.CONTAINER_DESKTOP, mCurrentScreen, lp.cellX, lp.cellY); + } + } + } + + public void onDragEnter(DragSource source, int x, int y, int xOffset, int yOffset, + Object dragInfo) { + clearVacantCache(); + } + + public void onDragOver(DragSource source, int x, int y, int xOffset, int yOffset, + Object dragInfo) { + } + + public void onDragExit(DragSource source, int x, int y, int xOffset, int yOffset, + Object dragInfo) { + clearVacantCache(); + } + + private void onDropExternal(int x, int y, Object dragInfo, CellLayout cellLayout) { + onDropExternal(x, y, dragInfo, cellLayout, false); + } + + private void onDropExternal(int x, int y, Object dragInfo, CellLayout cellLayout, + boolean insertAtFirst) { + // Drag from somewhere else + ItemInfo info = (ItemInfo) dragInfo; + + View view; + + switch (info.itemType) { + case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION: + case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT: + if (info.container == NO_ID) { + // Came from all apps -- make a copy + info = new ApplicationInfo((ApplicationInfo) info); + } + view = mLauncher.createShortcut(R.layout.application, cellLayout, + (ApplicationInfo) info); + break; + case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER: + view = FolderIcon.fromXml(R.layout.folder_icon, mLauncher, + (ViewGroup) getChildAt(mCurrentScreen), ((UserFolderInfo) info)); + break; + default: + throw new IllegalStateException("Unknown item type: " + info.itemType); + } + + cellLayout.addView(view, insertAtFirst ? 0 : -1); + view.setOnLongClickListener(mLongClickListener); + mTargetCell = estimateDropCell(x, y, 1, 1, view, cellLayout, mTargetCell); + cellLayout.onDropChild(view, mTargetCell); + CellLayout.LayoutParams lp = (CellLayout.LayoutParams) view.getLayoutParams(); + + final LauncherModel model = Launcher.getModel(); + model.addDesktopItem(info); + LauncherModel.addOrMoveItemInDatabase(mLauncher, info, + LauncherSettings.Favorites.CONTAINER_DESKTOP, mCurrentScreen, lp.cellX, lp.cellY); + } + + /** + * Return the current {@link CellLayout}, correctly picking the destination + * screen while a scroll is in progress. + */ + private CellLayout getCurrentDropLayout() { + int index = mScroller.isFinished() ? mCurrentScreen : mNextScreen; + return (CellLayout) getChildAt(index); + } + + /** + * {@inheritDoc} + */ + public boolean acceptDrop(DragSource source, int x, int y, + int xOffset, int yOffset, Object dragInfo) { + final CellLayout layout = getCurrentDropLayout(); + final CellLayout.CellInfo cellInfo = mDragInfo; + final int spanX = cellInfo == null ? 1 : cellInfo.spanX; + final int spanY = cellInfo == null ? 1 : cellInfo.spanY; + + if (mVacantCache == null) { + final View ignoreView = cellInfo == null ? null : cellInfo.cell; + mVacantCache = layout.findAllVacantCells(null, ignoreView); + } + + return mVacantCache.findCellForSpan(mTempEstimate, spanX, spanY, false); + } + + /** + * {@inheritDoc} + */ + public Rect estimateDropLocation(DragSource source, int x, int y, + int xOffset, int yOffset, Object dragInfo, Rect recycle) { + final CellLayout layout = getCurrentDropLayout(); + + final CellLayout.CellInfo cellInfo = mDragInfo; + final int spanX = cellInfo == null ? 1 : cellInfo.spanX; + final int spanY = cellInfo == null ? 1 : cellInfo.spanY; + final View ignoreView = cellInfo == null ? null : cellInfo.cell; + + final Rect location = recycle != null ? recycle : new Rect(); + + // Find drop cell and convert into rectangle + int[] dropCell = estimateDropCell(x - xOffset, y - yOffset, + spanX, spanY, ignoreView, layout, mTempCell); + + if (dropCell == null) { + return null; + } + + layout.cellToPoint(dropCell[0], dropCell[1], mTempEstimate); + location.left = mTempEstimate[0]; + location.top = mTempEstimate[1]; + + layout.cellToPoint(dropCell[0] + spanX, dropCell[1] + spanY, mTempEstimate); + location.right = mTempEstimate[0]; + location.bottom = mTempEstimate[1]; + + return location; + } + + /** + * Calculate the nearest cell where the given object would be dropped. + */ + private int[] estimateDropCell(int pixelX, int pixelY, + int spanX, int spanY, View ignoreView, CellLayout layout, int[] recycle) { + // Create vacant cell cache if none exists + if (mVacantCache == null) { + mVacantCache = layout.findAllVacantCells(null, ignoreView); + } + + // Find the best target drop location + return layout.findNearestVacantArea(pixelX, pixelY, + spanX, spanY, mVacantCache, recycle); + } + + void setLauncher(Launcher launcher) { + mLauncher = launcher; + } + + public void setDragger(DragController dragger) { + mDragger = dragger; + } + + public void onDropCompleted(View target, boolean success) { + if (success){ + if (target != this && mDragInfo != null) { + final CellLayout cellLayout = (CellLayout) getChildAt(mDragInfo.screen); + cellLayout.removeView(mDragInfo.cell); + final Object tag = mDragInfo.cell.getTag(); + Launcher.getModel().removeDesktopItem((ItemInfo) tag); + } + } else { + if (mDragInfo != null) { + final CellLayout cellLayout = (CellLayout) getChildAt(mDragInfo.screen); + cellLayout.onDropAborted(mDragInfo.cell); + } + } + + mDragInfo = null; + } + + public void scrollLeft() { + clearVacantCache(); + if (mNextScreen == INVALID_SCREEN && mCurrentScreen > 0 && mScroller.isFinished()) { + snapToScreen(mCurrentScreen - 1); + } + } + + public void scrollRight() { + clearVacantCache(); + if (mNextScreen == INVALID_SCREEN && mCurrentScreen < getChildCount() -1 && + mScroller.isFinished()) { + snapToScreen(mCurrentScreen + 1); + } + } + + public int getScreenForView(View v) { + int result = -1; + if (v != null) { + ViewParent vp = v.getParent(); + int count = getChildCount(); + for (int i = 0; i < count; i++) { + if (vp == getChildAt(i)) { + return i; + } + } + } + return result; + } + + /** + * Find a search widget on the given screen + */ + private Search findSearchWidget(CellLayout screen) { + final int count = screen.getChildCount(); + for (int i = 0; i < count; i++) { + View v = screen.getChildAt(i); + if (v instanceof Search) { + return (Search) v; + } + } + return null; + } + + /** + * Gets the first search widget on the current screen, if there is one. + * Returns <code>null</code> otherwise. + */ + public Search findSearchWidgetOnCurrentScreen() { + CellLayout currentScreen = (CellLayout)getChildAt(mCurrentScreen); + return findSearchWidget(currentScreen); + } + + public Folder getFolderForTag(Object tag) { + int screenCount = getChildCount(); + for (int screen = 0; screen < screenCount; screen++) { + CellLayout currentScreen = ((CellLayout) getChildAt(screen)); + int count = currentScreen.getChildCount(); + for (int i = 0; i < count; i++) { + View child = currentScreen.getChildAt(i); + CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams(); + if (lp.cellHSpan == 4 && lp.cellVSpan == 4 && child instanceof Folder) { + Folder f = (Folder) child; + if (f.getInfo() == tag) { + return f; + } + } + } + } + return null; + } + + public View getViewForTag(Object tag) { + int screenCount = getChildCount(); + for (int screen = 0; screen < screenCount; screen++) { + CellLayout currentScreen = ((CellLayout) getChildAt(screen)); + int count = currentScreen.getChildCount(); + for (int i = 0; i < count; i++) { + View child = currentScreen.getChildAt(i); + if (child.getTag() == tag) { + return child; + } + } + } + return null; + } + + /** + * Unlocks the SlidingDrawer so that touch events are processed. + * + * @see #lock() + */ + public void unlock() { + mLocked = false; + } + + /** + * Locks the SlidingDrawer so that touch events are ignores. + * + * @see #unlock() + */ + public void lock() { + mLocked = true; + } + + /** + * @return True is long presses are still allowed for the current touch + */ + public boolean allowLongPress() { + return mAllowLongPress; + } + + /** + * Set true to allow long-press events to be triggered, usually checked by + * {@link Launcher} to accept or block dpad-initiated long-presses. + */ + public void setAllowLongPress(boolean allowLongPress) { + mAllowLongPress = allowLongPress; + } + + void removeShortcutsForPackage(String packageName) { + final ArrayList<View> childrenToRemove = new ArrayList<View>(); + final LauncherModel model = Launcher.getModel(); + final int count = getChildCount(); + + for (int i = 0; i < count; i++) { + final CellLayout layout = (CellLayout) getChildAt(i); + int childCount = layout.getChildCount(); + + childrenToRemove.clear(); + + for (int j = 0; j < childCount; j++) { + final View view = layout.getChildAt(j); + Object tag = view.getTag(); + + if (tag instanceof ApplicationInfo) { + final ApplicationInfo info = (ApplicationInfo) tag; + // We need to check for ACTION_MAIN otherwise getComponent() might + // return null for some shortcuts (for instance, for shortcuts to + // web pages.) + final Intent intent = info.intent; + final ComponentName name = intent.getComponent(); + + if (Intent.ACTION_MAIN.equals(intent.getAction()) && + name != null && packageName.equals(name.getPackageName())) { + model.removeDesktopItem(info); + LauncherModel.deleteItemFromDatabase(mLauncher, info); + childrenToRemove.add(view); + } + } else if (tag instanceof UserFolderInfo) { + final UserFolderInfo info = (UserFolderInfo) tag; + final ArrayList<ApplicationInfo> contents = info.contents; + final ArrayList<ApplicationInfo> toRemove = new ArrayList<ApplicationInfo>(1); + final int contentsCount = contents.size(); + boolean removedFromFolder = false; + + for (int k = 0; k < contentsCount; k++) { + final ApplicationInfo appInfo = contents.get(k); + final Intent intent = appInfo.intent; + final ComponentName name = intent.getComponent(); + + if (Intent.ACTION_MAIN.equals(intent.getAction()) && + name != null && packageName.equals(name.getPackageName())) { + toRemove.add(appInfo); + LauncherModel.deleteItemFromDatabase(mLauncher, appInfo); + removedFromFolder = true; + } + } + + contents.removeAll(toRemove); + if (removedFromFolder) { + final Folder folder = getOpenFolder(); + if (folder != null) folder.notifyDataSetChanged(); + } + } + } + + childCount = childrenToRemove.size(); + for (int j = 0; j < childCount; j++) { + layout.removeViewInLayout(childrenToRemove.get(j)); + } + + if (childCount > 0) { + layout.requestLayout(); + layout.invalidate(); + } + } + } + + void updateShortcutsForPackage(String packageName) { + final int count = getChildCount(); + for (int i = 0; i < count; i++) { + final CellLayout layout = (CellLayout) getChildAt(i); + int childCount = layout.getChildCount(); + for (int j = 0; j < childCount; j++) { + final View view = layout.getChildAt(j); + Object tag = view.getTag(); + if (tag instanceof ApplicationInfo) { + ApplicationInfo info = (ApplicationInfo) tag; + // We need to check for ACTION_MAIN otherwise getComponent() might + // return null for some shortcuts (for instance, for shortcuts to + // web pages.) + final Intent intent = info.intent; + final ComponentName name = intent.getComponent(); + if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION && + Intent.ACTION_MAIN.equals(intent.getAction()) && name != null && + packageName.equals(name.getPackageName())) { + + final Drawable icon = Launcher.getModel().getApplicationInfoIcon( + mLauncher.getPackageManager(), info); + if (icon != null && icon != info.icon) { + info.icon.setCallback(null); + info.icon = Utilities.createIconThumbnail(icon, mContext); + info.filtered = true; + ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(null, + info.icon, null, null); + } + } + } + } + } + } + + void moveToDefaultScreen() { + snapToScreen(mDefaultScreen); + getChildAt(mDefaultScreen).requestFocus(); + } + + public static class SavedState extends BaseSavedState { + int currentScreen = -1; + + SavedState(Parcelable superState) { + super(superState); + } + + private SavedState(Parcel in) { + super(in); + currentScreen = in.readInt(); + } + + @Override + public void writeToParcel(Parcel out, int flags) { + super.writeToParcel(out, flags); + out.writeInt(currentScreen); + } + + public static final Parcelable.Creator<SavedState> CREATOR = + new Parcelable.Creator<SavedState>() { + public SavedState createFromParcel(Parcel in) { + return new SavedState(in); + } + + public SavedState[] newArray(int size) { + return new SavedState[size]; + } + }; + } +} |