blob: 72eeb223391db9bc1c6e80d085d8e6f1a6ad3cf0 [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;
21import java.util.Comparator;
22
Winson Chung80baf5a2010-08-09 16:03:15 -070023import android.animation.Animatable;
24import android.animation.AnimatableListenerAdapter;
25import android.animation.Animator;
26import android.animation.PropertyAnimator;
Winson Chung321e9ee2010-08-09 13:37:56 -070027import android.content.ComponentName;
28import android.content.Context;
29import android.content.res.TypedArray;
30import android.graphics.drawable.BitmapDrawable;
31import android.util.AttributeSet;
32import android.view.LayoutInflater;
33import android.view.View;
34import android.view.animation.AlphaAnimation;
35import android.view.animation.Animation;
36import android.view.animation.AnimationUtils;
37import android.view.animation.Animation.AnimationListener;
38import android.widget.TextView;
39
40import com.android.launcher.R;
41
42/**
43 * An implementation of PagedView that populates the pages of the workspace
44 * with all of the user's applications.
45 */
46public class AllAppsPagedView extends PagedView
Winson Chungb3347bb2010-08-19 14:51:28 -070047 implements AllAppsView, View.OnClickListener, View.OnLongClickListener, DragSource {
Winson Chung321e9ee2010-08-09 13:37:56 -070048
49 private static final String TAG = "AllAppsPagedView";
50 private static final boolean DEBUG = false;
51
52 private Launcher mLauncher;
53 private DragController mDragController;
54
55 // preserve compatibility with 3D all apps:
56 // 0.0 -> hidden
57 // 1.0 -> shown and opaque
58 // intermediate values -> partially shown & partially opaque
59 private float mZoom;
60
61 // set of all applications
62 private ArrayList<ApplicationInfo> mApps;
63 private ArrayList<ApplicationInfo> mFilteredApps;
64
65 // the types of applications to filter
66 static final int ALL_APPS_FLAG = -1;
67 private int mAppFilter = ALL_APPS_FLAG;
68
69 private int mCellCountX;
70 private int mCellCountY;
71
72 private final LayoutInflater mInflater;
73
74 public AllAppsPagedView(Context context) {
75 this(context, null);
76 }
77
78 public AllAppsPagedView(Context context, AttributeSet attrs) {
79 this(context, attrs, 0);
80 }
81
82 public AllAppsPagedView(Context context, AttributeSet attrs, int defStyle) {
83 super(context, attrs, defStyle);
84 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PagedView, defStyle, 0);
85 mCellCountX = a.getInt(R.styleable.PagedView_cellCountX, 6);
86 mCellCountY = a.getInt(R.styleable.PagedView_cellCountY, 4);
87 mInflater = LayoutInflater.from(context);
88 a.recycle();
89 setSoundEffectsEnabled(false);
90 }
91
92 @Override
93 public void setLauncher(Launcher launcher) {
94 mLauncher = launcher;
95 }
96
97 @Override
98 public void setDragController(DragController dragger) {
99 mDragController = dragger;
100 }
101
102 public void setAppFilter(int filterType) {
103 mAppFilter = filterType;
Winson Chung80baf5a2010-08-09 16:03:15 -0700104 if (mApps != null) {
105 mFilteredApps = rebuildFilteredApps(mApps);
106 setCurrentScreen(0);
107 invalidatePageData();
108 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700109 }
110
111 @Override
112 public void zoom(float zoom, boolean animate) {
113 mZoom = zoom;
114 cancelLongPress();
115
116 if (isVisible()) {
117 getParent().bringChildToFront(this);
118 setVisibility(View.VISIBLE);
119 if (animate) {
120 startAnimation(AnimationUtils.loadAnimation(getContext(),
121 R.anim.all_apps_2d_fade_in));
122 } else {
123 onAnimationEnd();
124 }
125 } else {
126 if (animate) {
127 startAnimation(AnimationUtils.loadAnimation(getContext(),
128 R.anim.all_apps_2d_fade_out));
129 } else {
130 onAnimationEnd();
131 }
132 }
133 }
134
135 protected void onAnimationEnd() {
136 if (!isVisible()) {
137 setVisibility(View.GONE);
138 mZoom = 0.0f;
139 } else {
140 mZoom = 1.0f;
141 }
142
143 if (mLauncher != null)
144 mLauncher.zoomed(mZoom);
145 }
146
147 private int getChildIndexForGrandChild(View v) {
148 final int childCount = getChildCount();
149 for (int i = 0; i < childCount; ++i) {
150 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i);
151 if (layout.indexOfChild(v) > -1) {
152 return i;
153 }
154 }
155 return -1;
156 }
157
158 @Override
159 public void onClick(View v) {
160 int childIndex = getChildIndexForGrandChild(v);
161 if (childIndex == getCurrentScreen()) {
162 final ApplicationInfo app = (ApplicationInfo) v.getTag();
163
Winson Chung80baf5a2010-08-09 16:03:15 -0700164 // animate some feedback to the click
165 animateClickFeedback(v, new Runnable() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700166 @Override
Winson Chung80baf5a2010-08-09 16:03:15 -0700167 public void run() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700168 mLauncher.startActivitySafely(app.intent, app);
169 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700170 });
Winson Chung321e9ee2010-08-09 13:37:56 -0700171 }
172 }
173
174 @Override
175 public boolean onLongClick(View v) {
176 if (!v.isInTouchMode()) {
177 return false;
178 }
179
180 ApplicationInfo app = (ApplicationInfo) v.getTag();
181 app = new ApplicationInfo(app);
182
183 mDragController.startDrag(v, this, app, DragController.DRAG_ACTION_COPY);
184 mLauncher.closeAllApps(true);
185 return true;
186 }
187
188 @Override
189 public void onDropCompleted(View target, boolean success) {
190 // do nothing
191 }
192
193 @Override
194 public boolean isVisible() {
195 return mZoom > 0.001f;
196 }
197
198 @Override
199 public boolean isAnimating() {
200 return (getAnimation() != null);
201 }
202
203 private ArrayList<ApplicationInfo> rebuildFilteredApps(ArrayList<ApplicationInfo> apps) {
204 ArrayList<ApplicationInfo> filteredApps = new ArrayList<ApplicationInfo>();
205 if (mAppFilter == ALL_APPS_FLAG) {
206 return apps;
207 } else {
208 final int length = apps.size();
209 for (int i = 0; i < length; ++i) {
210 ApplicationInfo info = apps.get(i);
211 if ((info.flags & mAppFilter) > 0) {
212 filteredApps.add(info);
213 }
214 }
215 }
216 return filteredApps;
217 }
218
219 @Override
220 public void setApps(ArrayList<ApplicationInfo> list) {
221 mApps = list;
Winson Chung80baf5a2010-08-09 16:03:15 -0700222 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung321e9ee2010-08-09 13:37:56 -0700223 mFilteredApps = rebuildFilteredApps(mApps);
224 invalidatePageData();
225 }
226
Winson Chung80baf5a2010-08-09 16:03:15 -0700227 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
228 // we add it in place, in alphabetical order
229 final int count = list.size();
230 for (int i = 0; i < count; ++i) {
231 final ApplicationInfo info = list.get(i);
232 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
233 if (index < 0) {
234 mApps.add(-(index + 1), info);
235 }
236 }
237 mFilteredApps = rebuildFilteredApps(mApps);
238 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700239 @Override
240 public void addApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700241 addAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700242 invalidatePageData();
243 }
244
Winson Chung80baf5a2010-08-09 16:03:15 -0700245 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700246 // loop through all the apps and remove apps that have the same component
247 final int length = list.size();
248 for (int i = 0; i < length; ++i) {
249 int removeIndex = findAppByComponent(mApps, list.get(i));
250 if (removeIndex > -1) {
251 mApps.remove(removeIndex);
252 }
253 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700254 mFilteredApps = rebuildFilteredApps(mApps);
255 }
256 @Override
257 public void removeApps(ArrayList<ApplicationInfo> list) {
258 removeAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700259 invalidatePageData();
260 }
261
262 @Override
263 public void updateApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700264 removeAppsWithoutInvalidate(list);
265 addAppsWithoutInvalidate(list);
266 invalidatePageData();
Winson Chung321e9ee2010-08-09 13:37:56 -0700267 }
268
269 private int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
270 ComponentName removeComponent = item.intent.getComponent();
271 final int length = list.size();
272 for (int i = 0; i < length; ++i) {
273 ApplicationInfo info = list.get(i);
274 if (info.intent.getComponent().equals(removeComponent)) {
275 return i;
276 }
277 }
278 return -1;
279 }
280
281 @Override
282 public void dumpState() {
283 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
284 }
285
286 @Override
287 public void surrender() {
288 // do nothing?
289 }
290
291 @Override
292 public void syncPages() {
293 // ensure that we have the right number of pages
294 int numPages = (int) Math.ceil((float) mFilteredApps.size() / (mCellCountX * mCellCountY));
295 int curNumPages = getChildCount();
296 // remove any extra pages after the "last" page
297 int extraPageDiff = curNumPages - numPages;
298 for (int i = 0; i < extraPageDiff; ++i) {
299 removeViewAt(numPages);
300 }
301 // add any necessary pages
302 for (int i = curNumPages; i < numPages; ++i) {
303 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
304 layout.setCellCount(mCellCountX, mCellCountY);
Winson Chung321e9ee2010-08-09 13:37:56 -0700305 addView(layout);
306 }
307
308 // bound the current page
309 setCurrentScreen(Math.max(0, Math.min(numPages - 1, getCurrentScreen())));
310 }
311
312 @Override
313 public void syncPageItems(int page) {
314 // ensure that we have the right number of items on the pages
Winson Chung80baf5a2010-08-09 16:03:15 -0700315 final int cellsPerPage = mCellCountX * mCellCountY;
316 final int startIndex = page * cellsPerPage;
317 final int endIndex = Math.min(startIndex + cellsPerPage, mFilteredApps.size());
Winson Chung321e9ee2010-08-09 13:37:56 -0700318 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700319
320 final int curNumPageItems = layout.getChildCount();
321 final int numPageItems = endIndex - startIndex;
322
323 // remove any extra items
324 int extraPageItemsDiff = curNumPageItems - numPageItems;
325 for (int i = 0; i < extraPageItemsDiff; ++i) {
326 layout.removeViewAt(numPageItems);
327 }
328 // add any necessary items
329 for (int i = curNumPageItems; i < numPageItems; ++i) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700330 TextView text = (TextView) mInflater.inflate(R.layout.all_apps_paged_view_application, layout, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700331 text.setOnClickListener(this);
332 text.setOnLongClickListener(this);
333
334 layout.addViewToCellLayout(text, -1, i,
335 new PagedViewCellLayout.LayoutParams(0, 0, 1, 1));
336 }
337
338 // actually reapply to the existing text views
339 for (int i = startIndex; i < endIndex; ++i) {
340 int index = i - startIndex;
341 ApplicationInfo info = mFilteredApps.get(i);
342 TextView text = (TextView) layout.getChildAt(index);
Winson Chung321e9ee2010-08-09 13:37:56 -0700343 text.setCompoundDrawablesWithIntrinsicBounds(null,
Winson Chungb3347bb2010-08-19 14:51:28 -0700344 new FastBitmapDrawable(info.iconBitmap), null, null);
Winson Chung321e9ee2010-08-09 13:37:56 -0700345 text.setText(info.title);
346 text.setTag(info);
Winson Chung321e9ee2010-08-09 13:37:56 -0700347
Winson Chung80baf5a2010-08-09 16:03:15 -0700348 PagedViewCellLayout.LayoutParams params =
349 (PagedViewCellLayout.LayoutParams) text.getLayoutParams();
350 params.cellX = index % mCellCountX;
351 params.cellY = index / mCellCountX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700352 }
353 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700354}