blob: 435004d3369a9cbe94dd2f22d44b47cca7473c7c [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;
24import android.widget.AdapterView;
25import android.widget.Button;
26import android.widget.LinearLayout;
27import android.widget.AbsListView;
Romain Guy574d20e2009-06-01 15:34:04 -070028import android.widget.BaseAdapter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.widget.AdapterView.OnItemClickListener;
30import android.widget.AdapterView.OnItemLongClickListener;
31
32/**
33 * Represents a set of icons chosen by the user or generated by the system.
34 */
35public class Folder extends LinearLayout implements DragSource, OnItemLongClickListener,
36 OnItemClickListener, OnClickListener, View.OnLongClickListener {
37
38 protected AbsListView mContent;
Joe Onorato00acb122009-08-04 16:04:30 -040039 protected DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040
41 protected Launcher mLauncher;
42
43 protected Button mCloseButton;
44
45 protected FolderInfo mInfo;
46
47 /**
48 * Which item is being dragged
49 */
Joe Onorato0589f0f2010-02-08 13:44:00 -080050 protected ShortcutInfo mDragItem;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051
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);
Romain Guyfb5411e2010-02-24 10:04:17 -080078 int[] pos = new int[2];
79 v.getLocationOnScreen(pos);
80 app.intent.setSourceBounds(new Rect(pos[0], pos[1],
81 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082 mLauncher.startActivitySafely(app.intent);
83 }
84
85 public void onClick(View v) {
86 mLauncher.closeFolder(this);
87 }
88
89 public boolean onLongClick(View v) {
90 mLauncher.closeFolder(this);
91 mLauncher.showRenameDialog(mInfo);
92 return true;
93 }
94
95 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
96 if (!view.isInTouchMode()) {
97 return false;
98 }
99
Joe Onorato0589f0f2010-02-08 13:44:00 -0800100 ShortcutInfo app = (ShortcutInfo) parent.getItemAtPosition(position);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101
Joe Onorato00acb122009-08-04 16:04:30 -0400102 mDragController.startDrag(view, this, app, DragController.DRAG_ACTION_COPY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103 mLauncher.closeFolder(this);
104 mDragItem = app;
105
106 return true;
107 }
108
Joe Onorato00acb122009-08-04 16:04:30 -0400109 public void setDragController(DragController dragController) {
110 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111 }
112
113 public void onDropCompleted(View target, boolean success) {
114 }
115
116 /**
117 * Sets the adapter used to populate the content area. The adapter must only
Joe Onorato0589f0f2010-02-08 13:44:00 -0800118 * contains ShortcutInfo items.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119 *
120 * @param adapter The list of applications to display in the folder.
121 */
Romain Guy574d20e2009-06-01 15:34:04 -0700122 void setContentAdapter(BaseAdapter adapter) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 mContent.setAdapter(adapter);
124 }
125
Romain Guy574d20e2009-06-01 15:34:04 -0700126 void notifyDataSetChanged() {
127 ((BaseAdapter) mContent.getAdapter()).notifyDataSetChanged();
128 }
129
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 void setLauncher(Launcher launcher) {
131 mLauncher = launcher;
132 }
133
134 /**
135 * @return the FolderInfo object associated with this folder
136 */
137 FolderInfo getInfo() {
138 return mInfo;
139 }
140
141 // When the folder opens, we need to refresh the GridView's selection by
142 // forcing a layout
143 void onOpen() {
144 mContent.requestLayout();
145 }
146
147 void onClose() {
148 final Workspace workspace = mLauncher.getWorkspace();
149 workspace.getChildAt(workspace.getCurrentScreen()).requestFocus();
150 }
151
152 void bind(FolderInfo info) {
153 mInfo = info;
154 mCloseButton.setText(info.title);
155 }
156}