blob: be3135c08f0ce71cf1453b3eb1b9c7a3f52575b8 [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);
Winson Chung321e9ee2010-08-09 13:37:56 -0700184 return true;
185 }
186
187 @Override
188 public void onDropCompleted(View target, boolean success) {
189 // do nothing
190 }
191
192 @Override
193 public boolean isVisible() {
194 return mZoom > 0.001f;
195 }
196
197 @Override
198 public boolean isAnimating() {
199 return (getAnimation() != null);
200 }
201
202 private ArrayList<ApplicationInfo> rebuildFilteredApps(ArrayList<ApplicationInfo> apps) {
203 ArrayList<ApplicationInfo> filteredApps = new ArrayList<ApplicationInfo>();
204 if (mAppFilter == ALL_APPS_FLAG) {
205 return apps;
206 } else {
207 final int length = apps.size();
208 for (int i = 0; i < length; ++i) {
209 ApplicationInfo info = apps.get(i);
210 if ((info.flags & mAppFilter) > 0) {
211 filteredApps.add(info);
212 }
213 }
214 }
215 return filteredApps;
216 }
217
218 @Override
219 public void setApps(ArrayList<ApplicationInfo> list) {
220 mApps = list;
Winson Chung80baf5a2010-08-09 16:03:15 -0700221 Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
Winson Chung321e9ee2010-08-09 13:37:56 -0700222 mFilteredApps = rebuildFilteredApps(mApps);
223 invalidatePageData();
224 }
225
Winson Chung80baf5a2010-08-09 16:03:15 -0700226 private void addAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
227 // we add it in place, in alphabetical order
228 final int count = list.size();
229 for (int i = 0; i < count; ++i) {
230 final ApplicationInfo info = list.get(i);
231 final int index = Collections.binarySearch(mApps, info, LauncherModel.APP_NAME_COMPARATOR);
232 if (index < 0) {
233 mApps.add(-(index + 1), info);
234 }
235 }
236 mFilteredApps = rebuildFilteredApps(mApps);
237 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700238 @Override
239 public void addApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700240 addAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700241 invalidatePageData();
242 }
243
Winson Chung80baf5a2010-08-09 16:03:15 -0700244 private void removeAppsWithoutInvalidate(ArrayList<ApplicationInfo> list) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700245 // loop through all the apps and remove apps that have the same component
246 final int length = list.size();
247 for (int i = 0; i < length; ++i) {
248 int removeIndex = findAppByComponent(mApps, list.get(i));
249 if (removeIndex > -1) {
250 mApps.remove(removeIndex);
251 }
252 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700253 mFilteredApps = rebuildFilteredApps(mApps);
254 }
255 @Override
256 public void removeApps(ArrayList<ApplicationInfo> list) {
257 removeAppsWithoutInvalidate(list);
Winson Chung321e9ee2010-08-09 13:37:56 -0700258 invalidatePageData();
259 }
260
261 @Override
262 public void updateApps(ArrayList<ApplicationInfo> list) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700263 removeAppsWithoutInvalidate(list);
264 addAppsWithoutInvalidate(list);
265 invalidatePageData();
Winson Chung321e9ee2010-08-09 13:37:56 -0700266 }
267
268 private int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) {
269 ComponentName removeComponent = item.intent.getComponent();
270 final int length = list.size();
271 for (int i = 0; i < length; ++i) {
272 ApplicationInfo info = list.get(i);
273 if (info.intent.getComponent().equals(removeComponent)) {
274 return i;
275 }
276 }
277 return -1;
278 }
279
280 @Override
281 public void dumpState() {
282 ApplicationInfo.dumpApplicationInfoList(TAG, "mApps", mApps);
283 }
284
285 @Override
286 public void surrender() {
287 // do nothing?
288 }
289
290 @Override
291 public void syncPages() {
292 // ensure that we have the right number of pages
293 int numPages = (int) Math.ceil((float) mFilteredApps.size() / (mCellCountX * mCellCountY));
294 int curNumPages = getChildCount();
295 // remove any extra pages after the "last" page
296 int extraPageDiff = curNumPages - numPages;
297 for (int i = 0; i < extraPageDiff; ++i) {
298 removeViewAt(numPages);
299 }
300 // add any necessary pages
301 for (int i = curNumPages; i < numPages; ++i) {
302 PagedViewCellLayout layout = new PagedViewCellLayout(getContext());
303 layout.setCellCount(mCellCountX, mCellCountY);
Winson Chung321e9ee2010-08-09 13:37:56 -0700304 addView(layout);
305 }
306
307 // bound the current page
308 setCurrentScreen(Math.max(0, Math.min(numPages - 1, getCurrentScreen())));
309 }
310
311 @Override
312 public void syncPageItems(int page) {
313 // ensure that we have the right number of items on the pages
Winson Chung80baf5a2010-08-09 16:03:15 -0700314 final int cellsPerPage = mCellCountX * mCellCountY;
315 final int startIndex = page * cellsPerPage;
316 final int endIndex = Math.min(startIndex + cellsPerPage, mFilteredApps.size());
Winson Chung321e9ee2010-08-09 13:37:56 -0700317 PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(page);
Winson Chung80baf5a2010-08-09 16:03:15 -0700318
319 final int curNumPageItems = layout.getChildCount();
320 final int numPageItems = endIndex - startIndex;
321
322 // remove any extra items
323 int extraPageItemsDiff = curNumPageItems - numPageItems;
324 for (int i = 0; i < extraPageItemsDiff; ++i) {
325 layout.removeViewAt(numPageItems);
326 }
327 // add any necessary items
328 for (int i = curNumPageItems; i < numPageItems; ++i) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700329 TextView text = (TextView) mInflater.inflate(R.layout.all_apps_paged_view_application, layout, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700330 text.setOnClickListener(this);
331 text.setOnLongClickListener(this);
332
333 layout.addViewToCellLayout(text, -1, i,
334 new PagedViewCellLayout.LayoutParams(0, 0, 1, 1));
335 }
336
337 // actually reapply to the existing text views
338 for (int i = startIndex; i < endIndex; ++i) {
339 int index = i - startIndex;
340 ApplicationInfo info = mFilteredApps.get(i);
341 TextView text = (TextView) layout.getChildAt(index);
Winson Chung321e9ee2010-08-09 13:37:56 -0700342 text.setCompoundDrawablesWithIntrinsicBounds(null,
Winson Chungb3347bb2010-08-19 14:51:28 -0700343 new FastBitmapDrawable(info.iconBitmap), null, null);
Winson Chung321e9ee2010-08-09 13:37:56 -0700344 text.setText(info.title);
345 text.setTag(info);
Winson Chung321e9ee2010-08-09 13:37:56 -0700346
Winson Chung80baf5a2010-08-09 16:03:15 -0700347 PagedViewCellLayout.LayoutParams params =
348 (PagedViewCellLayout.LayoutParams) text.getLayoutParams();
349 params.cellX = index % mCellCountX;
350 params.cellY = index / mCellCountX;
Winson Chung321e9ee2010-08-09 13:37:56 -0700351 }
352 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700353}