blob: 018b284563ee56eb8a98c3fff82c031765e03eee [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
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 Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
19import android.content.Context;
Romain Guyfb5411e2010-02-24 10:04:17 -080020import android.graphics.Rect;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.util.AttributeSet;
22import android.view.View;
23import android.view.View.OnClickListener;
Winson Chungaafa03c2010-06-11 17:34:16 -070024import android.widget.AbsListView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.widget.AdapterView;
Winson Chungaafa03c2010-06-11 17:34:16 -070026import android.widget.BaseAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.widget.Button;
28import android.widget.LinearLayout;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.widget.AdapterView.OnItemClickListener;
30import android.widget.AdapterView.OnItemLongClickListener;
31
Romain Guyedcce092010-03-04 13:03:17 -080032import com.android.launcher.R;
33
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034/**
35 * Represents a set of icons chosen by the user or generated by the system.
36 */
37public class Folder extends LinearLayout implements DragSource, OnItemLongClickListener,
38 OnItemClickListener, OnClickListener, View.OnLongClickListener {
39
40 protected AbsListView mContent;
Joe Onorato00acb122009-08-04 16:04:30 -040041 protected DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042
43 protected Launcher mLauncher;
44
45 protected Button mCloseButton;
46
47 protected FolderInfo mInfo;
48
49 /**
50 * Which item is being dragged
51 */
Joe Onorato0589f0f2010-02-08 13:44:00 -080052 protected ShortcutInfo mDragItem;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053
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 Onorato92442302009-09-21 15:37:59 -040069 mContent = (AbsListView) findViewById(R.id.folder_content);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070 mContent.setOnItemClickListener(this);
71 mContent.setOnItemLongClickListener(this);
72
Joe Onorato92442302009-09-21 15:37:59 -040073 mCloseButton = (Button) findViewById(R.id.folder_close);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074 mCloseButton.setOnClickListener(this);
75 mCloseButton.setOnLongClickListener(this);
76 }
77
78 public void onItemClick(AdapterView parent, View v, int position, long id) {
Joe Onorato0589f0f2010-02-08 13:44:00 -080079 ShortcutInfo app = (ShortcutInfo) parent.getItemAtPosition(position);
Romain Guyfb5411e2010-02-24 10:04:17 -080080 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 Onoratof984e852010-03-25 09:47:45 -070084 mLauncher.startActivitySafely(app.intent, app);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085 }
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 Onorato0589f0f2010-02-08 13:44:00 -0800102 ShortcutInfo app = (ShortcutInfo) parent.getItemAtPosition(position);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103
Joe Onorato00acb122009-08-04 16:04:30 -0400104 mDragController.startDrag(view, this, app, DragController.DRAG_ACTION_COPY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105 mLauncher.closeFolder(this);
106 mDragItem = app;
107
108 return true;
109 }
110
Joe Onorato00acb122009-08-04 16:04:30 -0400111 public void setDragController(DragController dragController) {
112 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 }
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 Onorato0589f0f2010-02-08 13:44:00 -0800120 * contains ShortcutInfo items.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 *
122 * @param adapter The list of applications to display in the folder.
123 */
Romain Guy574d20e2009-06-01 15:34:04 -0700124 void setContentAdapter(BaseAdapter adapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125 mContent.setAdapter(adapter);
126 }
127
Romain Guy574d20e2009-06-01 15:34:04 -0700128 void notifyDataSetChanged() {
129 ((BaseAdapter) mContent.getAdapter()).notifyDataSetChanged();
130 }
131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 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 Jurka0142d492010-08-25 17:46:15 -0700151 workspace.getChildAt(workspace.getCurrentPage()).requestFocus();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 }
153
154 void bind(FolderInfo info) {
155 mInfo = info;
156 mCloseButton.setText(info.title);
157 }
158}