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; |
| 20 | import android.util.AttributeSet; |
| 21 | import android.view.View; |
| 22 | import android.view.View.OnClickListener; |
| 23 | import android.widget.AdapterView; |
| 24 | import android.widget.Button; |
| 25 | import android.widget.LinearLayout; |
| 26 | import android.widget.AbsListView; |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 27 | import android.widget.BaseAdapter; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 28 | import android.widget.AdapterView.OnItemClickListener; |
| 29 | import android.widget.AdapterView.OnItemLongClickListener; |
| 30 | |
| 31 | /** |
| 32 | * Represents a set of icons chosen by the user or generated by the system. |
| 33 | */ |
| 34 | public class Folder extends LinearLayout implements DragSource, OnItemLongClickListener, |
| 35 | OnItemClickListener, OnClickListener, View.OnLongClickListener { |
| 36 | |
| 37 | protected AbsListView mContent; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 38 | protected DragController mDragController; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 39 | |
| 40 | protected Launcher mLauncher; |
| 41 | |
| 42 | protected Button mCloseButton; |
| 43 | |
| 44 | protected FolderInfo mInfo; |
| 45 | |
| 46 | /** |
| 47 | * Which item is being dragged |
| 48 | */ |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame^] | 49 | protected ShortcutInfo mDragItem; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 50 | private boolean mCloneInfo; |
| 51 | |
| 52 | /** |
| 53 | * Used to inflate the Workspace from XML. |
| 54 | * |
| 55 | * @param context The application's context. |
| 56 | * @param attrs The attribtues set containing the Workspace's customization values. |
| 57 | */ |
| 58 | public Folder(Context context, AttributeSet attrs) { |
| 59 | super(context, attrs); |
| 60 | setAlwaysDrawnWithCacheEnabled(false); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | protected void onFinishInflate() { |
| 65 | super.onFinishInflate(); |
| 66 | |
Joe Onorato | 9244230 | 2009-09-21 15:37:59 -0400 | [diff] [blame] | 67 | mContent = (AbsListView) findViewById(R.id.folder_content); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 68 | mContent.setOnItemClickListener(this); |
| 69 | mContent.setOnItemLongClickListener(this); |
| 70 | |
Joe Onorato | 9244230 | 2009-09-21 15:37:59 -0400 | [diff] [blame] | 71 | mCloseButton = (Button) findViewById(R.id.folder_close); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 72 | mCloseButton.setOnClickListener(this); |
| 73 | mCloseButton.setOnLongClickListener(this); |
| 74 | } |
| 75 | |
| 76 | public void onItemClick(AdapterView parent, View v, int position, long id) { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame^] | 77 | ShortcutInfo app = (ShortcutInfo) parent.getItemAtPosition(position); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 78 | mLauncher.startActivitySafely(app.intent); |
| 79 | } |
| 80 | |
| 81 | public void onClick(View v) { |
| 82 | mLauncher.closeFolder(this); |
| 83 | } |
| 84 | |
| 85 | public boolean onLongClick(View v) { |
| 86 | mLauncher.closeFolder(this); |
| 87 | mLauncher.showRenameDialog(mInfo); |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { |
| 92 | if (!view.isInTouchMode()) { |
| 93 | return false; |
| 94 | } |
| 95 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame^] | 96 | ShortcutInfo app = (ShortcutInfo) parent.getItemAtPosition(position); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 97 | if (mCloneInfo) { |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame^] | 98 | app = new ShortcutInfo(app); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 101 | mDragController.startDrag(view, this, app, DragController.DRAG_ACTION_COPY); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 102 | mLauncher.closeFolder(this); |
| 103 | mDragItem = app; |
| 104 | |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | void setCloneInfo(boolean cloneInfo) { |
| 109 | mCloneInfo = cloneInfo; |
| 110 | } |
| 111 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 112 | public void setDragController(DragController dragController) { |
| 113 | mDragController = dragController; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | public void onDropCompleted(View target, boolean success) { |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * 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^] | 121 | * contains ShortcutInfo items. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 122 | * |
| 123 | * @param adapter The list of applications to display in the folder. |
| 124 | */ |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 125 | void setContentAdapter(BaseAdapter adapter) { |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 126 | mContent.setAdapter(adapter); |
| 127 | } |
| 128 | |
Romain Guy | 574d20e | 2009-06-01 15:34:04 -0700 | [diff] [blame] | 129 | void notifyDataSetChanged() { |
| 130 | ((BaseAdapter) mContent.getAdapter()).notifyDataSetChanged(); |
| 131 | } |
| 132 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 133 | void setLauncher(Launcher launcher) { |
| 134 | mLauncher = launcher; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * @return the FolderInfo object associated with this folder |
| 139 | */ |
| 140 | FolderInfo getInfo() { |
| 141 | return mInfo; |
| 142 | } |
| 143 | |
| 144 | // When the folder opens, we need to refresh the GridView's selection by |
| 145 | // forcing a layout |
| 146 | void onOpen() { |
| 147 | mContent.requestLayout(); |
| 148 | } |
| 149 | |
| 150 | void onClose() { |
| 151 | final Workspace workspace = mLauncher.getWorkspace(); |
| 152 | workspace.getChildAt(workspace.getCurrentScreen()).requestFocus(); |
| 153 | } |
| 154 | |
| 155 | void bind(FolderInfo info) { |
| 156 | mInfo = info; |
| 157 | mCloseButton.setText(info.title); |
| 158 | } |
| 159 | } |