blob: a673304eee8b3ace4560221d079e8182abab5fc2 [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
2 * Copyright (C) 2010 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.launcher2;
18
19import java.util.ArrayList;
20import java.util.Collections;
Winson Chung321e9ee2010-08-09 13:37:56 -070021
22import android.content.ComponentName;
23import android.content.Context;
24import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070025import android.util.AttributeSet;
26import android.view.LayoutInflater;
27import android.view.View;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.view.animation.AnimationUtils;
Winson Chung321e9ee2010-08-09 13:37:56 -070029import android.widget.TextView;
30
31import com.android.launcher.R;
32
33/**
34 * An implementation of PagedView that populates the pages of the workspace
35 * with all of the user's applications.
36 */
37public class AllAppsPagedView extends PagedView
Winson Chungb3347bb2010-08-19 14:51:28 -070038 implements AllAppsView, View.OnClickListener, View.OnLongClickListener, DragSource {
Winson Chung321e9ee2010-08-09 13:37:56 -070039
40 private static final String TAG = "AllAppsPagedView";
41 private static final boolean DEBUG = false;
42
43 private Launcher mLauncher;
44 private DragController mDragController;
45
46 // preserve compatibility with 3D all apps:
47 // 0.0 -> hidden
48 // 1.0 -> shown and opaque
49 // intermediate values -> partially shown & partially opaque
50 private float mZoom;
51
52 // set of all applications
53 private ArrayList<ApplicationInfo> mApps;
54 private ArrayList<ApplicationInfo> mFilteredApps;
55
56 // the types of applications to filter
57 static final int ALL_APPS_FLAG = -1;
58 private int mAppFilter = ALL_APPS_FLAG;
59
60 private int mCellCountX;
61 private int mCellCountY;
62
63 private final LayoutInflater mInflater;
64
65 public AllAppsPagedView(Context context) {
66 this(context, null);
67 }
68
69 public AllAppsPagedView(Context context, AttributeSet attrs) {
70 this(context, attrs, 0);
71 }
72
73 public AllAppsPagedView(Context context, AttributeSet attrs, int defStyle) {
74 super(context, attrs, defStyle);
75 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
76 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6);
77 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
78 mInflater = LayoutInflater.from(context);
79 a.recycle();
80 setSoundEffectsEnabled(false);
81 }
82
83 @Override
84 public void setLauncher(Launcher launcher) {
85 mLauncher = launcher;
86 }
87
88 @Override
89 public void setDragController(DragController dragger) {
90 mDragController = dragger;
91 }
92
93 public void setAppFilter(int filterType) {
94 mAppFilter = filterType;
Winson Chung80baf5a2010-08-09 16:03:15 -070095 if (mApps != null) {
96 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung86f77532010-08-24 11:08:22 -070097 setCurrentPage(0);
Winson Chung80baf5a2010-08-09 16:03:15 -070098 invalidatePageData();
99 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700100 }
101
102 @Override
103 public void zoom(float zoom, boolean animate) {
104 mZoom = zoom;
105 cancelLongPress();
106
107 if (isVisible()) {
108 getParent().bringChildToFront(this);
109 setVisibility(View.VISIBLE);
110 if (animate) {
111 startAnimation(AnimationUtils.loadAnimation(getContext(),
112 R.anim.all_apps_2d_fade_in));
113 } else {
114 onAnimationEnd();
115 }
116 } else {
117 if (animate) {
118 startAnimation(AnimationUtils.loadAnimation(getContext(),
119 R.anim.all_apps_2d_fade_out));
120 } else {
121 onAnimationEnd();
122 }
123 }
124 }
125
126 protected void onAnimationEnd() {
127 if (!isVisible()) {
128 setVisibility(View.GONE);
129 mZoom = 0.0f;
130 } else {
131 mZoom = 1.0f;
132 }
133
134 if (mLauncher != null)
135 mLauncher.zoomed(mZoom);
136 }
137
138 private int getChildIndexForGrandChild(View v) {
139 final int childCount = getChildCount();
140 for (int i = 0; i < childCount; ++i) {
141 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
142 if (layout.indexOfChild(v) > -1) {
143 return i;
144 }
145 }
146 return -1;
147 }
148
149 @Override
150 public void onClick(View v) {
151 int childIndex = getChildIndexForGrandChild(v);
Winson Chung86f77532010-08-24 11:08:22 -0700152 if (childIndex == getCurrentPage()) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700153 final ApplicationInfo app = (ApplicationInfo) v.getTag();
154
Winson Chung80baf5a2010-08-09 16:03:15 -0700155 // animate some feedback to the click
156 animateClickFeedback(v, new Runnable() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700157 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700158 public void run() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700159 mLauncher.startActivitySafely(app.intent, app);
160 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700161 });
Winson Chung321e9ee2010-08-09 13:37:56 -0700162 }
163 }
164
165 @Override
166 public boolean onLongClick(View v) {
167 if (!v.isInTouchMode()) {
168 return false;
169 }
170
171 ApplicationInfo app = (ApplicationInfo) v.getTag();
172 app = new ApplicationInfo(app);
173
174 mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY);
Winson Chung321e9ee2010-08-09 13:37:56 -0700175 return true;
176 }
177
178 @Override
179 public void onDropCompleted(View target, boolean success) {
180 // do nothing
181 }
182
183 @Override
184 public boolean isVisible() {
185 return mZoom > 0.001f;
186 }
187
188 @Override
189 public boolean isAnimating() {
190 return (getAnimation() != null);
191 }
192
193 private ArrayList<ApplicationInfo> rebuildFilteredApps(ArrayList<ApplicationInfo> apps) {
194 ArrayList<ApplicationInfo> filteredApps = new ArrayList<ApplicationInfo>();
195 if (mAppFilter == ALL_APPS_FLAG) {
196 return apps;
197 } else {
198 final int length = apps.size();
199 for (int i = 0; i < length; ++i) {
200 ApplicationInfo info = apps.get(i);
201 if ((info.flags & mAppFilter) > 0) {
202 filteredApps.add(info);
203 }
204 }
205 }
206 return filteredApps;
207 }
208
209 @Override
210 public void setApps(ArrayList<ApplicationInfo> list) {
211 mApps = list;
Winson Chung80baf5a2010-08-09 16:03:15 -0700212 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung321e9ee2010-08-09 13:37:56 -0700213 mFilteredApps = rebuildFilteredApps(mApps);
Winson Chung241c3b42010-08-25 16:53:03 -0700214 mPageViewIconCache.clear();
Winson Chung321e9ee2010-08-09 13:37:56 -0700215 invalidatePageData();
216 }
217
Winson Chung80baf5a2010-08-09 16:03:15 -0700218 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
219 // we add it in place, in alphabetical order
220 final int count = list.size();
221 for (int i = 0; i < count; ++i) {
222 final ApplicationInfo info = list.get(i);
223 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
224 if (index < 0) {
225 mApps.add(-(index + 1), info);
226 }
227 }
228 mFilteredApps = rebuildFilteredApps(mApps);
229 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700230 @Override
231 public void addApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700232 addAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700233 invalidatePageData();
234 }
235
Winson Chung80baf5a2010-08-09 16:03:15 -0700236 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700237 // loop through all the apps and remove apps that have the same component
238 final int length = list.size();
239 for (int i = 0; i < length; ++i) {
Winson Chung241c3b42010-08-25 16:53:03 -0700240 final ApplicationInfo info = list.get(i);
241 int removeIndex = findAppByComponent(mApps, info);
Winson Chung321e9ee2010-08-09 13:37:56 -0700242 if (removeIndex > -1) {
243 mApps.remove(removeIndex);
Winson Chung241c3b42010-08-25 16:53:03 -0700244 mPageViewIconCache.removeOutline(info);
Winson Chung321e9ee2010-08-09 13:37:56 -0700245 }
246 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700247 mFilteredApps = rebuildFilteredApps(mApps);
248 }
249 @Override
250 public void removeApps(ArrayList<ApplicationInfo> list) {
251 removeAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700252 invalidatePageData();
253 }
254
255 @Override
256 public void updateApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700257 removeAppsWithoutInvalidate(list);
258 addAppsWithoutInvalidate(list);
259 invalidatePageData();
Winson Chung321e9ee2010-08-09 13:37:56 -0700260 }
261
262 private int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
263 ComponentName removeComponent = item.intent.getComponent();
264 final int length = list.size();
265 for (int i = 0; i < length; ++i) {
266 ApplicationInfo info = list.get(i);
267 if (info.intent.getComponent().equals(removeComponent)) {
268 return i;
269 }
270 }
271 return -1;
272 }
273
274 @Override
275 public void dumpState() {
276 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
277 }
278
279 @Override
280 public void surrender() {
281 // do nothing?
282 }
283
284 @Override
285 public void syncPages() {
286 // ensure that we have the right number of pages
287 int numPages = (int) Math.ceil((float) mFilteredApps.size() / (mCellCountX * mCellCountY));
288 int curNumPages = getChildCount();
289 // remove any extra pages after the "last" page
290 int extraPageDiff = curNumPages - numPages;
291 for (int i = 0; i < extraPageDiff; ++i) {
292 removeViewAt(numPages);
293 }
294 // add any necessary pages
295 for (int i = curNumPages; i < numPages; ++i) {
296 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
297 layout.setCellCount(mCellCountX, mCellCountY);
Winson Chung321e9ee2010-08-09 13:37:56 -0700298 addView(layout);
299 }
300
301 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700302 setCurrentPage(Math.max(0, Math.min(numPages - 1, getCurrentPage())));
Winson Chung321e9ee2010-08-09 13:37:56 -0700303 }
304
305 @Override
306 public void syncPageItems(int page) {
307 // ensure that we have the right number of items on the pages
Winson Chung80baf5a2010-08-09 16:03:15 -0700308 final int cellsPerPage = mCellCountX * mCellCountY;
309 final int startIndex = page * cellsPerPage;
310 final int endIndex = Math.min(startIndex + cellsPerPage, mFilteredApps.size());
Winson Chung321e9ee2010-08-09 13:37:56 -0700311 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700312
313 final int curNumPageItems = layout.getChildCount();
314 final int numPageItems = endIndex - startIndex;
315
316 // remove any extra items
317 int extraPageItemsDiff = curNumPageItems - numPageItems;
318 for (int i = 0; i < extraPageItemsDiff; ++i) {
319 layout.removeViewAt(numPageItems);
320 }
321 // add any necessary items
322 for (int i = curNumPageItems; i < numPageItems; ++i) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700323 TextView text = (TextView) mInflater.inflate(R.layout.all_apps_paged_view_application, layout, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700324 text.setOnClickListener(this);
325 text.setOnLongClickListener(this);
326
327 layout.addViewToCellLayout(text, -1, i,
328 new PagedViewCellLayout.LayoutParams(0, 0, 1, 1));
329 }
330
331 // actually reapply to the existing text views
332 for (int i = startIndex; i < endIndex; ++i) {
Winson Chung241c3b42010-08-25 16:53:03 -0700333 final int index = i - startIndex;
334 final ApplicationInfo info = mFilteredApps.get(i);
335 PagedViewIcon icon = (PagedViewIcon) layout.getChildAt(index);
336 icon.applyFromApplicationInfo(info, mPageViewIconCache);
Winson Chung321e9ee2010-08-09 13:37:56 -0700337
Winson Chung80baf5a2010-08-09 16:03:15 -0700338 PagedViewCellLayout.LayoutParams params =
Winson Chung241c3b42010-08-25 16:53:03 -0700339 (PagedViewCellLayout.LayoutParams) icon.getLayoutParams();
Winson Chung80baf5a2010-08-09 16:03:15 -0700340 params.cellX = index % mCellCountX;
341 params.cellY = index / mCellCountX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700342 }
343 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700344}