Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher2; |
| 18 | |
Patrick Dubroy | 558654c | 2010-07-23 16:48:11 -0700 | [diff] [blame] | 19 | import com.android.launcher.R; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 20 | |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
| 23 | import android.content.res.Resources; |
Daniel Sandler | 86b4054 | 2010-06-01 14:48:12 -0700 | [diff] [blame] | 24 | import android.graphics.Bitmap; |
Patrick Dubroy | 558654c | 2010-07-23 16:48:11 -0700 | [diff] [blame] | 25 | import android.graphics.drawable.BitmapDrawable; |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 26 | import android.util.AttributeSet; |
| 27 | import android.util.Log; |
| 28 | import android.view.KeyEvent; |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 29 | import android.view.LayoutInflater; |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 30 | import android.view.View; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 31 | import android.view.ViewGroup; |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 32 | import android.view.animation.AnimationUtils; |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 33 | import android.widget.AdapterView; |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 34 | import android.widget.ArrayAdapter; |
| 35 | import android.widget.GridView; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 36 | import android.widget.ImageButton; |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 37 | import android.widget.RelativeLayout; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 38 | import android.widget.TextView; |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 39 | |
Patrick Dubroy | 558654c | 2010-07-23 16:48:11 -0700 | [diff] [blame] | 40 | import java.util.ArrayList; |
| 41 | import java.util.Collections; |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 42 | |
| 43 | public class AllApps2D |
| 44 | extends RelativeLayout |
| 45 | implements AllAppsView, |
| 46 | AdapterView.OnItemClickListener, |
| 47 | AdapterView.OnItemLongClickListener, |
| 48 | View.OnKeyListener, |
| 49 | DragSource { |
| 50 | |
| 51 | private static final String TAG = "Launcher.AllApps2D"; |
Daniel Sandler | 86b4054 | 2010-06-01 14:48:12 -0700 | [diff] [blame] | 52 | private static final boolean DEBUG = false; |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 53 | |
| 54 | private Launcher mLauncher; |
| 55 | private DragController mDragController; |
| 56 | |
| 57 | private GridView mGrid; |
| 58 | |
Patrick Dubroy | 3d605d5 | 2010-07-29 13:59:29 -0700 | [diff] [blame] | 59 | /** All applications in the system (we might only be showing a subset) */ |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 60 | private ArrayList<ApplicationInfo> mAllAppsList = new ArrayList<ApplicationInfo>(); |
| 61 | |
Patrick Dubroy | 3d605d5 | 2010-07-29 13:59:29 -0700 | [diff] [blame] | 62 | /** Currently visible applications in the grid */ |
| 63 | private ArrayList<ApplicationInfo> mVisibleAppsList = new ArrayList<ApplicationInfo>(); |
| 64 | |
| 65 | public enum AppType { APP, GAME, DOWNLOADED, ALL }; |
| 66 | |
| 67 | private AppType mCurrentFilter = AppType.ALL; |
| 68 | |
Daniel Sandler | 73a0554 | 2010-03-09 14:45:57 -0500 | [diff] [blame] | 69 | // preserve compatibility with 3D all apps: |
| 70 | // 0.0 -> hidden |
| 71 | // 1.0 -> shown and opaque |
| 72 | // intermediate values -> partially shown & partially opaque |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 73 | private float mZoom; |
| 74 | |
| 75 | private AppsAdapter mAppsAdapter; |
| 76 | |
| 77 | // ------------------------------------------------------------ |
Daniel Sandler | 73a0554 | 2010-03-09 14:45:57 -0500 | [diff] [blame] | 78 | |
| 79 | public static class HomeButton extends ImageButton { |
| 80 | public HomeButton(Context context, AttributeSet attrs) { |
| 81 | super(context, attrs); |
| 82 | } |
| 83 | @Override |
| 84 | public View focusSearch(int direction) { |
| 85 | if (direction == FOCUS_UP) return super.focusSearch(direction); |
| 86 | return null; |
| 87 | } |
| 88 | } |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 89 | |
| 90 | public class AppsAdapter extends ArrayAdapter<ApplicationInfo> { |
| 91 | private final LayoutInflater mInflater; |
| 92 | |
| 93 | public AppsAdapter(Context context, ArrayList<ApplicationInfo> apps) { |
| 94 | super(context, 0, apps); |
| 95 | mInflater = LayoutInflater.from(context); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public View getView(int position, View convertView, ViewGroup parent) { |
| 100 | final ApplicationInfo info = getItem(position); |
| 101 | |
| 102 | if (convertView == null) { |
| 103 | convertView = mInflater.inflate(R.layout.application_boxed, parent, false); |
| 104 | } |
| 105 | |
| 106 | // if (!info.filtered) { |
| 107 | // info.icon = Utilities.createIconThumbnail(info.icon, getContext()); |
| 108 | // info.filtered = true; |
| 109 | // } |
| 110 | |
| 111 | final TextView textView = (TextView) convertView; |
Daniel Sandler | 86b4054 | 2010-06-01 14:48:12 -0700 | [diff] [blame] | 112 | if (DEBUG) { |
| 113 | Log.d(TAG, "icon bitmap = " + info.iconBitmap |
| 114 | + " density = " + info.iconBitmap.getDensity()); |
| 115 | } |
| 116 | info.iconBitmap.setDensity(Bitmap.DENSITY_NONE); |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 117 | textView.setCompoundDrawablesWithIntrinsicBounds(null, new BitmapDrawable(info.iconBitmap), null, null); |
| 118 | textView.setText(info.title); |
| 119 | |
| 120 | return convertView; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | public AllApps2D(Context context, AttributeSet attrs) { |
| 125 | super(context, attrs); |
| 126 | setVisibility(View.GONE); |
| 127 | setSoundEffectsEnabled(false); |
| 128 | |
Patrick Dubroy | 3d605d5 | 2010-07-29 13:59:29 -0700 | [diff] [blame] | 129 | mAppsAdapter = new AppsAdapter(getContext(), mVisibleAppsList); |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
| 133 | protected void onFinishInflate() { |
Daniel Sandler | c351eb8 | 2010-03-03 15:05:19 -0500 | [diff] [blame] | 134 | try { |
| 135 | mGrid = (GridView)findViewWithTag("all_apps_2d_grid"); |
| 136 | if (mGrid == null) throw new Resources.NotFoundException(); |
| 137 | mGrid.setOnItemClickListener(this); |
| 138 | mGrid.setOnItemLongClickListener(this); |
| 139 | |
Patrick Dubroy | 558654c | 2010-07-23 16:48:11 -0700 | [diff] [blame] | 140 | // The home button is optional; some layouts might not use it |
Daniel Sandler | c351eb8 | 2010-03-03 15:05:19 -0500 | [diff] [blame] | 141 | ImageButton homeButton = (ImageButton) findViewWithTag("all_apps_2d_home"); |
Patrick Dubroy | 558654c | 2010-07-23 16:48:11 -0700 | [diff] [blame] | 142 | if (homeButton != null) { |
| 143 | homeButton.setOnClickListener( |
| 144 | new View.OnClickListener() { |
| 145 | public void onClick(View v) { |
Winson Chung | 097eb0a | 2011-03-18 16:56:08 -0700 | [diff] [blame] | 146 | mLauncher.showWorkspace(true); |
Patrick Dubroy | 558654c | 2010-07-23 16:48:11 -0700 | [diff] [blame] | 147 | } |
| 148 | }); |
| 149 | } |
Daniel Sandler | c351eb8 | 2010-03-03 15:05:19 -0500 | [diff] [blame] | 150 | } catch (Resources.NotFoundException e) { |
| 151 | Log.e(TAG, "Can't find necessary layout elements for AllApps2D"); |
| 152 | } |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 153 | |
Daniel Sandler | c351eb8 | 2010-03-03 15:05:19 -0500 | [diff] [blame] | 154 | setOnKeyListener(this); |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | public AllApps2D(Context context, AttributeSet attrs, int defStyle) { |
| 158 | this(context, attrs); |
| 159 | } |
| 160 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame^] | 161 | @Override |
| 162 | public void setup(Launcher launcher, DragController dragController) { |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 163 | mLauncher = launcher; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame^] | 164 | mDragController = dragController; |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 168 | if (!isVisible()) return false; |
| 169 | |
| 170 | switch (keyCode) { |
| 171 | case KeyEvent.KEYCODE_BACK: |
Winson Chung | 097eb0a | 2011-03-18 16:56:08 -0700 | [diff] [blame] | 172 | mLauncher.showWorkspace(true); |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 173 | break; |
| 174 | default: |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | public void onItemClick(AdapterView parent, View v, int position, long id) { |
| 182 | ApplicationInfo app = (ApplicationInfo) parent.getItemAtPosition(position); |
Joe Onorato | f984e85 | 2010-03-25 09:47:45 -0700 | [diff] [blame] | 183 | mLauncher.startActivitySafely(app.intent, app); |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { |
| 187 | if (!view.isInTouchMode()) { |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | ApplicationInfo app = (ApplicationInfo) parent.getItemAtPosition(position); |
| 192 | app = new ApplicationInfo(app); |
| 193 | |
| 194 | mDragController.startDrag(view, this, app, DragController.DRAG_ACTION_COPY); |
Winson Chung | 097eb0a | 2011-03-18 16:56:08 -0700 | [diff] [blame] | 195 | mLauncher.showWorkspace(true); |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 196 | |
| 197 | return true; |
| 198 | } |
| 199 | |
Daniel Sandler | 73a0554 | 2010-03-09 14:45:57 -0500 | [diff] [blame] | 200 | protected void onFocusChanged(boolean gainFocus, int direction, android.graphics.Rect prev) { |
| 201 | if (gainFocus) { |
| 202 | mGrid.requestFocus(); |
| 203 | } |
| 204 | } |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 205 | |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 206 | @Override |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 207 | public void onDragViewVisible() { |
| 208 | } |
| 209 | |
| 210 | @Override |
Patrick Dubroy | 5f44542 | 2011-02-18 14:35:21 -0800 | [diff] [blame] | 211 | public void onDropCompleted(View target, Object dragInfo, boolean success) { |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Zoom to the specifed level. |
| 216 | * |
| 217 | * @param zoom [0..1] 0 is hidden, 1 is open |
| 218 | */ |
| 219 | public void zoom(float zoom, boolean animate) { |
| 220 | // Log.d(TAG, "zooming " + ((zoom == 1.0) ? "open" : "closed")); |
| 221 | cancelLongPress(); |
| 222 | |
| 223 | mZoom = zoom; |
| 224 | |
| 225 | if (isVisible()) { |
| 226 | getParent().bringChildToFront(this); |
| 227 | setVisibility(View.VISIBLE); |
| 228 | mGrid.setAdapter(mAppsAdapter); |
| 229 | if (animate) { |
| 230 | startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.all_apps_2d_fade_in)); |
| 231 | } else { |
| 232 | onAnimationEnd(); |
| 233 | } |
| 234 | } else { |
| 235 | if (animate) { |
| 236 | startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.all_apps_2d_fade_out)); |
| 237 | } else { |
| 238 | onAnimationEnd(); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | protected void onAnimationEnd() { |
| 244 | if (!isVisible()) { |
| 245 | setVisibility(View.GONE); |
| 246 | mGrid.setAdapter(null); |
| 247 | mZoom = 0.0f; |
| 248 | } else { |
| 249 | mZoom = 1.0f; |
| 250 | } |
Daniel Sandler | c351eb8 | 2010-03-03 15:05:19 -0500 | [diff] [blame] | 251 | |
| 252 | mLauncher.zoomed(mZoom); |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | public boolean isVisible() { |
| 256 | return mZoom > 0.001f; |
| 257 | } |
| 258 | |
Patrick Dubroy | ff5f040 | 2010-07-26 15:23:26 -0700 | [diff] [blame] | 259 | public boolean isAnimating() { |
| 260 | return (getAnimation() != null); |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | public void setApps(ArrayList<ApplicationInfo> list) { |
| 264 | mAllAppsList.clear(); |
| 265 | addApps(list); |
Patrick Dubroy | 3d605d5 | 2010-07-29 13:59:29 -0700 | [diff] [blame] | 266 | filterApps(mCurrentFilter); |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | public void addApps(ArrayList<ApplicationInfo> list) { |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 270 | final int N = list.size(); |
| 271 | |
| 272 | for (int i=0; i<N; i++) { |
| 273 | final ApplicationInfo item = list.get(i); |
| 274 | int index = Collections.binarySearch(mAllAppsList, item, |
| 275 | LauncherModel.APP_NAME_COMPARATOR); |
| 276 | if (index < 0) { |
| 277 | index = -(index+1); |
| 278 | } |
| 279 | mAllAppsList.add(index, item); |
| 280 | } |
Patrick Dubroy | 3d605d5 | 2010-07-29 13:59:29 -0700 | [diff] [blame] | 281 | filterApps(mCurrentFilter); |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | public void removeApps(ArrayList<ApplicationInfo> list) { |
| 285 | final int N = list.size(); |
Patrick Dubroy | 3d605d5 | 2010-07-29 13:59:29 -0700 | [diff] [blame] | 286 | |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 287 | for (int i=0; i<N; i++) { |
| 288 | final ApplicationInfo item = list.get(i); |
| 289 | int index = findAppByComponent(mAllAppsList, item); |
| 290 | if (index >= 0) { |
| 291 | mAllAppsList.remove(index); |
| 292 | } else { |
| 293 | Log.w(TAG, "couldn't find a match for item \"" + item + "\""); |
| 294 | // Try to recover. This should keep us from crashing for now. |
| 295 | } |
| 296 | } |
Patrick Dubroy | 3d605d5 | 2010-07-29 13:59:29 -0700 | [diff] [blame] | 297 | filterApps(mCurrentFilter); |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 298 | } |
| 299 | |
Joe Onorato | 64e6be7 | 2010-03-05 15:05:52 -0500 | [diff] [blame] | 300 | public void updateApps(ArrayList<ApplicationInfo> list) { |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 301 | // Just remove and add, because they may need to be re-sorted. |
| 302 | removeApps(list); |
| 303 | addApps(list); |
| 304 | } |
| 305 | |
Patrick Dubroy | 3d605d5 | 2010-07-29 13:59:29 -0700 | [diff] [blame] | 306 | public void filterApps(AppType appType) { |
| 307 | mCurrentFilter = appType; |
| 308 | |
| 309 | mAppsAdapter.setNotifyOnChange(false); |
| 310 | mVisibleAppsList.clear(); |
| 311 | if (appType == AppType.ALL) { |
| 312 | mVisibleAppsList.addAll(mAllAppsList); |
Patrick Dubroy | cd95371 | 2011-02-28 15:16:42 -0800 | [diff] [blame] | 313 | } else if (appType == AppType.DOWNLOADED) { |
Patrick Dubroy | 3d605d5 | 2010-07-29 13:59:29 -0700 | [diff] [blame] | 314 | for (ApplicationInfo info : mAllAppsList) { |
Patrick Dubroy | cd95371 | 2011-02-28 15:16:42 -0800 | [diff] [blame] | 315 | if ((info.flags & ApplicationInfo.DOWNLOADED_FLAG) != 0) { |
Patrick Dubroy | 3d605d5 | 2010-07-29 13:59:29 -0700 | [diff] [blame] | 316 | mVisibleAppsList.add(info); |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | mAppsAdapter.notifyDataSetChanged(); |
| 321 | } |
| 322 | |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 323 | private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) { |
| 324 | ComponentName component = item.intent.getComponent(); |
| 325 | final int N = list.size(); |
| 326 | for (int i=0; i<N; i++) { |
| 327 | ApplicationInfo x = list.get(i); |
| 328 | if (x.intent.getComponent().equals(component)) { |
| 329 | return i; |
| 330 | } |
| 331 | } |
| 332 | return -1; |
| 333 | } |
| 334 | |
| 335 | public void dumpState() { |
| 336 | ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList", mAllAppsList); |
| 337 | } |
Romain Guy | 13c2e7b | 2010-03-10 19:45:00 -0800 | [diff] [blame] | 338 | |
| 339 | public void surrender() { |
| 340 | } |
Winson Chung | 337cd9d | 2011-03-30 10:39:30 -0700 | [diff] [blame] | 341 | |
| 342 | public void reset() { |
| 343 | // Do nothing |
| 344 | } |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | |