blob: 1a91eb134b162e47c028c93d7b6fd4803b69ae4b [file] [log] [blame]
The Android Open Source Projectc8f00b62008-10-21 07:00:00 -07001/*
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.ArrayAdapter;
25import android.widget.Button;
26import android.widget.GridView;
27import android.widget.LinearLayout;
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 GridView 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 = (GridView) 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 return false;
87 }
88
89 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
90 if (!view.isInTouchMode()) {
91 return false;
92 }
93
94 ApplicationInfo app = (ApplicationInfo) parent.getItemAtPosition(position);
95 if (mCloneInfo) {
96 app = new ApplicationInfo(app);
97 }
98
99 mDragger.startDrag(view, this, app, DragController.DRAG_ACTION_COPY);
100 mLauncher.closeFolder(this);
101 mDragItem = app;
102
103 return true;
104 }
105
106 void setCloneInfo(boolean cloneInfo) {
107 mCloneInfo = cloneInfo;
108 }
109
110 public void setDragger(DragController dragger) {
111 mDragger = dragger;
112 }
113
114 public void onDropCompleted(View target, boolean success) {
115 }
116
117 /**
118 * Sets the adapter used to populate the content area. The adapter must only
119 * contains ApplicationInfo items.
120 *
121 * @param adapter The list of applications to display in the folder.
122 */
123 void setContentAdapter(ArrayAdapter<ApplicationInfo> adapter) {
124 mContent.setAdapter(adapter);
125 }
126
127 void setLauncher(Launcher launcher) {
128 mLauncher = launcher;
129 }
130
131 /**
132 * @return the FolderInfo object associated with this folder
133 */
134 FolderInfo getInfo() {
135 return mInfo;
136 }
137
138 // When the folder opens, we need to refresh the GridView's selection by
139 // forcing a layout
140 void onOpen() {
141 mContent.requestLayout();
142 }
143
144 void onClose() {
145 final Workspace workspace = mLauncher.getWorkspace();
146 workspace.getChildAt(workspace.getCurrentScreen()).requestFocus();
147 }
148}