blob: 378c2488c2aaa632ef895ff0f8bb9b4c76b291d7 [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);
214 invalidatePageData();
215 }
216
Winson Chung80baf5a2010-08-09 16:03:15 -0700217 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
218 // we add it in place, in alphabetical order
219 final int count = list.size();
220 for (int i = 0; i < count; ++i) {
221 final ApplicationInfo info = list.get(i);
222 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
223 if (index < 0) {
224 mApps.add(-(index + 1), info);
225 }
226 }
227 mFilteredApps = rebuildFilteredApps(mApps);
228 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700229 @Override
230 public void addApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700231 addAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700232 invalidatePageData();
233 }
234
Winson Chung80baf5a2010-08-09 16:03:15 -0700235 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700236 // loop through all the apps and remove apps that have the same component
237 final int length = list.size();
238 for (int i = 0; i < length; ++i) {
239 int removeIndex = findAppByComponent(mApps, list.get(i));
240 if (removeIndex > -1) {
241 mApps.remove(removeIndex);
242 }
243 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700244 mFilteredApps = rebuildFilteredApps(mApps);
245 }
246 @Override
247 public void removeApps(ArrayList<ApplicationInfo> list) {
248 removeAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700249 invalidatePageData();
250 }
251
252 @Override
253 public void updateApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700254 removeAppsWithoutInvalidate(list);
255 addAppsWithoutInvalidate(list);
256 invalidatePageData();
Winson Chung321e9ee2010-08-09 13:37:56 -0700257 }
258
259 private int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
260 ComponentName removeComponent = item.intent.getComponent();
261 final int length = list.size();
262 for (int i = 0; i < length; ++i) {
263 ApplicationInfo info = list.get(i);
264 if (info.intent.getComponent().equals(removeComponent)) {
265 return i;
266 }
267 }
268 return -1;
269 }
270
271 @Override
272 public void dumpState() {
273 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
274 }
275
276 @Override
277 public void surrender() {
278 // do nothing?
279 }
280
281 @Override
282 public void syncPages() {
283 // ensure that we have the right number of pages
284 int numPages = (int) Math.ceil((float) mFilteredApps.size() / (mCellCountX * mCellCountY));
285 int curNumPages = getChildCount();
286 // remove any extra pages after the "last" page
287 int extraPageDiff = curNumPages - numPages;
288 for (int i = 0; i < extraPageDiff; ++i) {
289 removeViewAt(numPages);
290 }
291 // add any necessary pages
292 for (int i = curNumPages; i < numPages; ++i) {
293 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
294 layout.setCellCount(mCellCountX, mCellCountY);
Winson Chung321e9ee2010-08-09 13:37:56 -0700295 addView(layout);
296 }
297
298 // bound the current page
Winson Chung86f77532010-08-24 11:08:22 -0700299 setCurrentPage(Math.max(0, Math.min(numPages - 1, getCurrentPage())));
Winson Chung321e9ee2010-08-09 13:37:56 -0700300 }
301
302 @Override
303 public void syncPageItems(int page) {
304 // ensure that we have the right number of items on the pages
Winson Chung80baf5a2010-08-09 16:03:15 -0700305 final int cellsPerPage = mCellCountX * mCellCountY;
306 final int startIndex = page * cellsPerPage;
307 final int endIndex = Math.min(startIndex + cellsPerPage, mFilteredApps.size());
Winson Chung321e9ee2010-08-09 13:37:56 -0700308 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700309
310 final int curNumPageItems = layout.getChildCount();
311 final int numPageItems = endIndex - startIndex;
312
313 // remove any extra items
314 int extraPageItemsDiff = curNumPageItems - numPageItems;
315 for (int i = 0; i < extraPageItemsDiff; ++i) {
316 layout.removeViewAt(numPageItems);
317 }
318 // add any necessary items
319 for (int i = curNumPageItems; i < numPageItems; ++i) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700320 TextView text = (TextView) mInflater.inflate(R.layout.all_apps_paged_view_application, layout, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700321 text.setOnClickListener(this);
322 text.setOnLongClickListener(this);
323
324 layout.addViewToCellLayout(text, -1, i,
325 new PagedViewCellLayout.LayoutParams(0, 0, 1, 1));
326 }
327
328 // actually reapply to the existing text views
329 for (int i = startIndex; i < endIndex; ++i) {
330 int index = i - startIndex;
331 ApplicationInfo info = mFilteredApps.get(i);
332 TextView text = (TextView) layout.getChildAt(index);
Winson Chung321e9ee2010-08-09 13:37:56 -0700333 text.setCompoundDrawablesWithIntrinsicBounds(null,
Winson Chungb3347bb2010-08-19 14:51:28 -0700334 new FastBitmapDrawable(info.iconBitmap), null, null);
Winson Chung321e9ee2010-08-09 13:37:56 -0700335 text.setText(info.title);
336 text.setTag(info);
Winson Chung321e9ee2010-08-09 13:37:56 -0700337
Winson Chung80baf5a2010-08-09 16:03:15 -0700338 PagedViewCellLayout.LayoutParams params =
339 (PagedViewCellLayout.LayoutParams) text.getLayoutParams();
340 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}