The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Joe Onorato | a590252 | 2009-07-30 13:37:37 -0700 | [diff] [blame] | 17 | package com.android.launcher2; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
Romain Guy | fb5411e | 2010-02-24 10:04:17 -0800 | [diff] [blame] | 20 | import android.graphics.Rect; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 21 | import android.util.AttributeSet; |
| 22 | import android.view.View; |
| 23 | import android.view.View.OnClickListener; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 24 | import android.widget.AbsListView; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 25 | import android.widget.AdapterView; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 26 | import android.widget.BaseAdapter; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 27 | import android.widget.Button; |
| 28 | import android.widget.LinearLayout; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 29 | import android.widget.AdapterView.OnItemClickListener; |
| 30 | import android.widget.AdapterView.OnItemLongClickListener; |
| 31 | |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 32 | import com.android.launcher.R; |
| 33 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 34 | /** |
| 35 | * Represents a set of icons chosen by the user or generated by the system. |
| 36 | */ |
| 37 | public class Folder extends LinearLayout implements DragSource, OnItemLongClickListener, |
| 38 | OnItemClickListener, OnClickListener, View.OnLongClickListener { |
| 39 | |
| 40 | protected AbsListView mContent; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 41 | protected DragController mDragController; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 42 | |
| 43 | protected Launcher mLauncher; |
| 44 | |
| 45 | protected Button mCloseButton; |
| 46 | |
| 47 | protected FolderInfo mInfo; |
| 48 | |
| 49 | /** |
| 50 | * Which item is being dragged |
| 51 | */ |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 52 | protected ShortcutInfo mDragItem; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Used to inflate the Workspace from XML. |
| 56 | * |
| 57 | * @param context The application's context. |
| 58 | * @param attrs The attribtues set containing the Workspace's customization values. |
| 59 | */ |
| 60 | public Folder(Context context, AttributeSet attrs) { |
| 61 | super(context, attrs); |
| 62 | setAlwaysDrawnWithCacheEnabled(false); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | protected void onFinishInflate() { |
| 67 | super.onFinishInflate(); |
| 68 | |
Joe Onorato | 9244230 | 2009-09-21 15:37:59 -0400 | [diff] [blame] | 69 | mContent = (AbsListView) findViewById(R.id.folder_content); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 70 | mContent.setOnItemClickListener(this); |
| 71 | mContent.setOnItemLongClickListener(this); |
| 72 | |
Joe Onorato | 9244230 | 2009-09-21 15:37:59 -0400 | [diff] [blame] | 73 | mCloseButton = (Button) findViewById(R.id.folder_close); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 74 | mCloseButton.setOnClickListener(this); |
| 75 | mCloseButton.setOnLongClickListener(this); |
| 76 | } |
| 77 | |
| 78 | public void onItemClick(AdapterView parent, View v, int position, long id) { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 79 | ShortcutInfo app = (ShortcutInfo) parent.getItemAtPosition(position); |
Romain Guy | fb5411e | 2010-02-24 10:04:17 -0800 | [diff] [blame] | 80 | int[] pos = new int[2]; |
| 81 | v.getLocationOnScreen(pos); |
| 82 | app.intent.setSourceBounds(new Rect(pos[0], pos[1], |
| 83 | pos[0] + v.getWidth(), pos[1] + v.getHeight())); |
Joe Onorato | f984e85 | 2010-03-25 09:47:45 -0700 | [diff] [blame] | 84 | mLauncher.startActivitySafely(app.intent, app); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | public void onClick(View v) { |
| 88 | mLauncher.closeFolder(this); |
| 89 | } |
| 90 | |
| 91 | public boolean onLongClick(View v) { |
| 92 | mLauncher.closeFolder(this); |
| 93 | mLauncher.showRenameDialog(mInfo); |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { |
| 98 | if (!view.isInTouchMode()) { |
| 99 | return false; |
| 100 | } |
| 101 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 102 | ShortcutInfo app = (ShortcutInfo) parent.getItemAtPosition(position); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 103 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 104 | mDragController.startDrag(view, this, app, DragController.DRAG_ACTION_COPY); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 105 | mLauncher.closeFolder(this); |
| 106 | mDragItem = app; |
| 107 | |
| 108 | return true; |
| 109 | } |
| 110 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 111 | public void setDragController(DragController dragController) { |
| 112 | mDragController = dragController; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | public void onDropCompleted(View target, boolean success) { |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Sets the adapter used to populate the content area. The adapter must only |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 120 | * contains ShortcutInfo items. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 121 | * |
| 122 | * @param adapter The list of applications to display in the folder. |
| 123 | */ |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 124 | void setContentAdapter(BaseAdapter adapter) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 125 | mContent.setAdapter(adapter); |
| 126 | } |
| 127 | |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 128 | void notifyDataSetChanged() { |
| 129 | ((BaseAdapter) mContent.getAdapter()).notifyDataSetChanged(); |
| 130 | } |
| 131 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 132 | void setLauncher(Launcher launcher) { |
| 133 | mLauncher = launcher; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * @return the FolderInfo object associated with this folder |
| 138 | */ |
| 139 | FolderInfo getInfo() { |
| 140 | return mInfo; |
| 141 | } |
| 142 | |
| 143 | // When the folder opens, we need to refresh the GridView's selection by |
| 144 | // forcing a layout |
| 145 | void onOpen() { |
| 146 | mContent.requestLayout(); |
| 147 | } |
| 148 | |
| 149 | void onClose() { |
| 150 | final Workspace workspace = mLauncher.getWorkspace(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 151 | workspace.getChildAt(workspace.getCurrentPage()).requestFocus(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void bind(FolderInfo info) { |
| 155 | mInfo = info; |
| 156 | mCloseButton.setText(info.title); |
| 157 | } |
| 158 | } |