blob: bcbccf71b3c034481b094f5a04a6ad51593b66c8 [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
17package com.android.launcher;
18
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;
27import android.widget.ListAdapter;
28import 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;
38 protected DragController mDragger;
39
40 protected Launcher mLauncher;
41
42 protected Button mCloseButton;
43
44 protected FolderInfo mInfo;
45
46 /**
47 * Which item is being dragged
48 */
49 protected ApplicationInfo mDragItem;
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
67 mContent = (AbsListView) findViewById(R.id.content);
68 mContent.setOnItemClickListener(this);
69 mContent.setOnItemLongClickListener(this);
70
71 mCloseButton = (Button) findViewById(R.id.close);
72 mCloseButton.setOnClickListener(this);
73 mCloseButton.setOnLongClickListener(this);
74 }
75
76 public void onItemClick(AdapterView parent, View v, int position, long id) {
77 ApplicationInfo app = (ApplicationInfo) parent.getItemAtPosition(position);
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
96 ApplicationInfo app = (ApplicationInfo) parent.getItemAtPosition(position);
97 if (mCloneInfo) {
98 app = new ApplicationInfo(app);
99 }
100
101 mDragger.startDrag(view, this, app, DragController.DRAG_ACTION_COPY);
102 mLauncher.closeFolder(this);
103 mDragItem = app;
104
105 return true;
106 }
107
108 void setCloneInfo(boolean cloneInfo) {
109 mCloneInfo = cloneInfo;
110 }
111
112 public void setDragger(DragController dragger) {
113 mDragger = dragger;
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
121 * contains ApplicationInfo items.
122 *
123 * @param adapter The list of applications to display in the folder.
124 */
125 void setContentAdapter(ListAdapter adapter) {
126 mContent.setAdapter(adapter);
127 }
128
129 void setLauncher(Launcher launcher) {
130 mLauncher = launcher;
131 }
132
133 /**
134 * @return the FolderInfo object associated with this folder
135 */
136 FolderInfo getInfo() {
137 return mInfo;
138 }
139
140 // When the folder opens, we need to refresh the GridView's selection by
141 // forcing a layout
142 void onOpen() {
143 mContent.requestLayout();
144 }
145
146 void onClose() {
147 final Workspace workspace = mLauncher.getWorkspace();
148 workspace.getChildAt(workspace.getCurrentScreen()).requestFocus();
149 }
150
151 void bind(FolderInfo info) {
152 mInfo = info;
153 mCloseButton.setText(info.title);
154 }
155}