blob: 7c35f7997a62de68be664723dd1f3e6723fbf0b8 [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;
20import android.util.AttributeSet;
21import android.view.View;
22import android.view.View.OnClickListener;
23import android.widget.AdapterView;
24import android.widget.Button;
25import android.widget.LinearLayout;
26import android.widget.AbsListView;
Romain Guy574d20e2009-06-01 15:34:04 -070027import android.widget.BaseAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.widget.AdapterView.OnItemClickListener;
29import android.widget.AdapterView.OnItemLongClickListener;
30
31/**
32 * Represents a set of icons chosen by the user or generated by the system.
33 */
34public class Folder extends LinearLayout implements DragSource, OnItemLongClickListener,
35 OnItemClickListener, OnClickListener, View.OnLongClickListener {
36
37 protected AbsListView mContent;
Joe Onorato00acb122009-08-04 16:04:30 -040038 protected DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039
40 protected Launcher mLauncher;
41
42 protected Button mCloseButton;
43
44 protected FolderInfo mInfo;
45
46 /**
47 * Which item is being dragged
48 */
Joe Onorato0589f0f2010-02-08 13:44:00 -080049 protected ShortcutInfo mDragItem;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050 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 Onorato92442302009-09-21 15:37:59 -040067 mContent = (AbsListView) findViewById(R.id.folder_content);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068 mContent.setOnItemClickListener(this);
69 mContent.setOnItemLongClickListener(this);
70
Joe Onorato92442302009-09-21 15:37:59 -040071 mCloseButton = (Button) findViewById(R.id.folder_close);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072 mCloseButton.setOnClickListener(this);
73 mCloseButton.setOnLongClickListener(this);
74 }
75
76 public void onItemClick(AdapterView parent, View v, int position, long id) {
Joe Onorato0589f0f2010-02-08 13:44:00 -080077 ShortcutInfo app = (ShortcutInfo) parent.getItemAtPosition(position);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078 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 Onorato0589f0f2010-02-08 13:44:00 -080096 ShortcutInfo app = (ShortcutInfo) parent.getItemAtPosition(position);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097 if (mCloneInfo) {
Joe Onorato0589f0f2010-02-08 13:44:00 -080098 app = new ShortcutInfo(app);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099 }
100
Joe Onorato00acb122009-08-04 16:04:30 -0400101 mDragController.startDrag(view, this, app, DragController.DRAG_ACTION_COPY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102 mLauncher.closeFolder(this);
103 mDragItem = app;
104
105 return true;
106 }
107
108 void setCloneInfo(boolean cloneInfo) {
109 mCloneInfo = cloneInfo;
110 }
111
Joe Onorato00acb122009-08-04 16:04:30 -0400112 public void setDragController(DragController dragController) {
113 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114 }
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 Onorato0589f0f2010-02-08 13:44:00 -0800121 * contains ShortcutInfo items.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122 *
123 * @param adapter The list of applications to display in the folder.
124 */
Romain Guy574d20e2009-06-01 15:34:04 -0700125 void setContentAdapter(BaseAdapter adapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 mContent.setAdapter(adapter);
127 }
128
Romain Guy574d20e2009-06-01 15:34:04 -0700129 void notifyDataSetChanged() {
130 ((BaseAdapter) mContent.getAdapter()).notifyDataSetChanged();
131 }
132
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 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}