blob: 7eb3bb162712a3cd5021130b9a2fa5dc5f8ea6bd [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.widget.AdapterView.OnItemClickListener;
28import android.widget.AdapterView.OnItemLongClickListener;
29
Romain Guyedcce092010-03-04 13:03:17 -080030import com.android.launcher.R;
31
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032/**
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
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
51 /**
52 * Used to inflate the Workspace from XML.
53 *
54 * @param context The application's context.
55 * @param attrs The attribtues set containing the Workspace's customization values.
56 */
57 public Folder(Context context, AttributeSet attrs) {
58 super(context, attrs);
59 setAlwaysDrawnWithCacheEnabled(false);
60 }
61
62 @Override
63 protected void onFinishInflate() {
64 super.onFinishInflate();
65
Joe Onorato92442302009-09-21 15:37:59 -040066 mCloseButton = (Button) findViewById(R.id.folder_close);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067 mCloseButton.setOnClickListener(this);
68 mCloseButton.setOnLongClickListener(this);
69 }
70
71 public void onItemClick(AdapterView parent, View v, int position, long id) {
Joe Onorato0589f0f2010-02-08 13:44:00 -080072 ShortcutInfo app = (ShortcutInfo) parent.getItemAtPosition(position);
Romain Guyfb5411e2010-02-24 10:04:17 -080073 int[] pos = new int[2];
74 v.getLocationOnScreen(pos);
75 app.intent.setSourceBounds(new Rect(pos[0], pos[1],
76 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -070077 mLauncher.startActivitySafely(app.intent, app);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078 }
79
80 public void onClick(View v) {
81 mLauncher.closeFolder(this);
82 }
83
84 public boolean onLongClick(View v) {
85 mLauncher.closeFolder(this);
86 mLauncher.showRenameDialog(mInfo);
87 return true;
88 }
89
90 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
91 if (!view.isInTouchMode()) {
92 return false;
93 }
94
Joe Onorato0589f0f2010-02-08 13:44:00 -080095 ShortcutInfo app = (ShortcutInfo) parent.getItemAtPosition(position);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096
Joe Onorato00acb122009-08-04 16:04:30 -040097 mDragController.startDrag(view, this, app, DragController.DRAG_ACTION_COPY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098 mLauncher.closeFolder(this);
99 mDragItem = app;
100
101 return true;
102 }
103
Joe Onorato00acb122009-08-04 16:04:30 -0400104 public void setDragController(DragController dragController) {
105 mDragController = dragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106 }
107
Patrick Dubroya669d792010-11-23 14:40:33 -0800108 @Override
Patrick Dubroy5f445422011-02-18 14:35:21 -0800109 public void onDropCompleted(View target, Object dragInfo, boolean success) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 }
111
Patrick Dubroya669d792010-11-23 14:40:33 -0800112 @Override
113 public void onDragViewVisible() {
114 }
115
Romain Guy574d20e2009-06-01 15:34:04 -0700116 void notifyDataSetChanged() {
Romain Guy574d20e2009-06-01 15:34:04 -0700117 }
118
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119 void setLauncher(Launcher launcher) {
120 mLauncher = launcher;
121 }
122
123 /**
124 * @return the FolderInfo object associated with this folder
125 */
126 FolderInfo getInfo() {
127 return mInfo;
128 }
129
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 void onOpen() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 }
132
133 void onClose() {
134 final Workspace workspace = mLauncher.getWorkspace();
Michael Jurka0142d492010-08-25 17:46:15 -0700135 workspace.getChildAt(workspace.getCurrentPage()).requestFocus();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 }
137
138 void bind(FolderInfo info) {
139 mInfo = info;
140 mCloseButton.setText(info.title);
141 }
142}