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 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.content.Context; |
| 21 | import android.content.res.Resources; |
| 22 | import android.graphics.Bitmap; |
| 23 | import android.graphics.Canvas; |
| 24 | import android.graphics.PixelFormat; |
| 25 | import android.graphics.Rect; |
| 26 | import android.graphics.drawable.BitmapDrawable; |
| 27 | import android.os.SystemClock; |
| 28 | import android.util.AttributeSet; |
| 29 | import android.util.Log; |
| 30 | import android.view.KeyEvent; |
| 31 | import android.view.ViewGroup; |
| 32 | import android.view.MotionEvent; |
| 33 | import android.view.LayoutInflater; |
| 34 | import android.view.SoundEffectConstants; |
| 35 | import android.view.SurfaceHolder; |
| 36 | import android.view.VelocityTracker; |
| 37 | import android.view.View; |
| 38 | import android.view.animation.Animation; |
| 39 | import android.view.animation.AnimationUtils; |
| 40 | import android.view.ViewConfiguration; |
| 41 | import android.view.accessibility.AccessibilityEvent; |
| 42 | import android.widget.AdapterView; |
| 43 | import android.widget.ImageButton; |
| 44 | import android.widget.TextView; |
| 45 | import android.widget.ArrayAdapter; |
| 46 | import android.widget.GridView; |
| 47 | import android.widget.RelativeLayout; |
| 48 | |
| 49 | import java.util.ArrayList; |
| 50 | import java.util.Arrays; |
| 51 | import java.util.Collections; |
| 52 | import java.util.Comparator; |
| 53 | |
| 54 | |
| 55 | public class AllApps2D |
| 56 | extends RelativeLayout |
| 57 | implements AllAppsView, |
| 58 | AdapterView.OnItemClickListener, |
| 59 | AdapterView.OnItemLongClickListener, |
| 60 | View.OnKeyListener, |
| 61 | DragSource { |
| 62 | |
| 63 | private static final String TAG = "Launcher.AllApps2D"; |
| 64 | |
| 65 | private Launcher mLauncher; |
| 66 | private DragController mDragController; |
| 67 | |
| 68 | private GridView mGrid; |
| 69 | |
| 70 | private ArrayList<ApplicationInfo> mAllAppsList = new ArrayList<ApplicationInfo>(); |
| 71 | |
| 72 | /** |
| 73 | * True when we are using arrow keys or trackball to drive navigation |
| 74 | */ |
| 75 | private boolean mArrowNavigation = false; |
| 76 | private boolean mStartedScrolling; |
| 77 | |
| 78 | /** |
| 79 | * Used to keep track of the selection when AllApps2D loses window focus. |
| 80 | * One of the SELECTION_ constants. |
| 81 | */ |
| 82 | private int mLastSelection; |
| 83 | |
| 84 | /** |
| 85 | * Used to keep track of the selection when AllApps2D loses window focus |
| 86 | */ |
| 87 | private int mLastSelectedIcon; |
| 88 | |
| 89 | private float mZoom; |
| 90 | |
| 91 | private AppsAdapter mAppsAdapter; |
| 92 | |
| 93 | // ------------------------------------------------------------ |
| 94 | |
| 95 | public class AppsAdapter extends ArrayAdapter<ApplicationInfo> { |
| 96 | private final LayoutInflater mInflater; |
| 97 | |
| 98 | public AppsAdapter(Context context, ArrayList<ApplicationInfo> apps) { |
| 99 | super(context, 0, apps); |
| 100 | mInflater = LayoutInflater.from(context); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public View getView(int position, View convertView, ViewGroup parent) { |
| 105 | final ApplicationInfo info = getItem(position); |
| 106 | |
| 107 | if (convertView == null) { |
| 108 | convertView = mInflater.inflate(R.layout.application_boxed, parent, false); |
| 109 | } |
| 110 | |
| 111 | // if (!info.filtered) { |
| 112 | // info.icon = Utilities.createIconThumbnail(info.icon, getContext()); |
| 113 | // info.filtered = true; |
| 114 | // } |
| 115 | |
| 116 | final TextView textView = (TextView) convertView; |
| 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 | |
| 129 | mAppsAdapter = new AppsAdapter(getContext(), mAllAppsList); |
| 130 | mAppsAdapter.setNotifyOnChange(false); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | protected void onFinishInflate() { |
| 135 | setBackgroundColor(0xFF000000); |
| 136 | |
Daniel Sandler | c351eb8 | 2010-03-03 15:05:19 -0500 | [diff] [blame^] | 137 | try { |
| 138 | mGrid = (GridView)findViewWithTag("all_apps_2d_grid"); |
| 139 | if (mGrid == null) throw new Resources.NotFoundException(); |
| 140 | mGrid.setOnItemClickListener(this); |
| 141 | mGrid.setOnItemLongClickListener(this); |
| 142 | |
| 143 | ImageButton homeButton = (ImageButton) findViewWithTag("all_apps_2d_home"); |
| 144 | if (homeButton == null) throw new Resources.NotFoundException(); |
| 145 | homeButton.setOnClickListener( |
| 146 | new View.OnClickListener() { |
| 147 | public void onClick(View v) { |
| 148 | mLauncher.closeAllApps(true); |
| 149 | } |
| 150 | }); |
| 151 | } catch (Resources.NotFoundException e) { |
| 152 | Log.e(TAG, "Can't find necessary layout elements for AllApps2D"); |
| 153 | } |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 154 | |
Daniel Sandler | c351eb8 | 2010-03-03 15:05:19 -0500 | [diff] [blame^] | 155 | setOnKeyListener(this); |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | public AllApps2D(Context context, AttributeSet attrs, int defStyle) { |
| 159 | this(context, attrs); |
| 160 | } |
| 161 | |
| 162 | public void setLauncher(Launcher launcher) { |
| 163 | mLauncher = launcher; |
| 164 | } |
| 165 | |
| 166 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 167 | if (!isVisible()) return false; |
| 168 | |
| 169 | switch (keyCode) { |
| 170 | case KeyEvent.KEYCODE_BACK: |
| 171 | mLauncher.closeAllApps(true); |
| 172 | break; |
| 173 | default: |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | public void onItemClick(AdapterView parent, View v, int position, long id) { |
| 181 | ApplicationInfo app = (ApplicationInfo) parent.getItemAtPosition(position); |
| 182 | mLauncher.startActivitySafely(app.intent); |
| 183 | } |
| 184 | |
| 185 | public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { |
| 186 | if (!view.isInTouchMode()) { |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | ApplicationInfo app = (ApplicationInfo) parent.getItemAtPosition(position); |
| 191 | app = new ApplicationInfo(app); |
| 192 | |
| 193 | mDragController.startDrag(view, this, app, DragController.DRAG_ACTION_COPY); |
| 194 | mLauncher.closeAllApps(true); |
| 195 | |
| 196 | return true; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | public void setDragController(DragController dragger) { |
| 201 | mDragController = dragger; |
| 202 | } |
| 203 | |
| 204 | public void onDropCompleted(View target, boolean success) { |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Zoom to the specifed level. |
| 209 | * |
| 210 | * @param zoom [0..1] 0 is hidden, 1 is open |
| 211 | */ |
| 212 | public void zoom(float zoom, boolean animate) { |
| 213 | // Log.d(TAG, "zooming " + ((zoom == 1.0) ? "open" : "closed")); |
| 214 | cancelLongPress(); |
| 215 | |
| 216 | mZoom = zoom; |
| 217 | |
| 218 | if (isVisible()) { |
| 219 | getParent().bringChildToFront(this); |
| 220 | setVisibility(View.VISIBLE); |
| 221 | mGrid.setAdapter(mAppsAdapter); |
| 222 | if (animate) { |
| 223 | startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.all_apps_2d_fade_in)); |
| 224 | } else { |
| 225 | onAnimationEnd(); |
| 226 | } |
| 227 | } else { |
| 228 | if (animate) { |
| 229 | startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.all_apps_2d_fade_out)); |
| 230 | } else { |
| 231 | onAnimationEnd(); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | protected void onAnimationEnd() { |
| 237 | if (!isVisible()) { |
| 238 | setVisibility(View.GONE); |
| 239 | mGrid.setAdapter(null); |
| 240 | mZoom = 0.0f; |
| 241 | } else { |
| 242 | mZoom = 1.0f; |
| 243 | } |
Daniel Sandler | c351eb8 | 2010-03-03 15:05:19 -0500 | [diff] [blame^] | 244 | |
| 245 | mLauncher.zoomed(mZoom); |
Daniel Sandler | 388f679 | 2010-03-02 14:08:08 -0500 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | public boolean isVisible() { |
| 249 | return mZoom > 0.001f; |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public boolean isOpaque() { |
| 254 | return mZoom > 0.999f; |
| 255 | } |
| 256 | |
| 257 | public void setApps(ArrayList<ApplicationInfo> list) { |
| 258 | mAllAppsList.clear(); |
| 259 | addApps(list); |
| 260 | } |
| 261 | |
| 262 | public void addApps(ArrayList<ApplicationInfo> list) { |
| 263 | // Log.d(TAG, "addApps: " + list.size() + " apps: " + list.toString()); |
| 264 | |
| 265 | final int N = list.size(); |
| 266 | |
| 267 | for (int i=0; i<N; i++) { |
| 268 | final ApplicationInfo item = list.get(i); |
| 269 | int index = Collections.binarySearch(mAllAppsList, item, |
| 270 | LauncherModel.APP_NAME_COMPARATOR); |
| 271 | if (index < 0) { |
| 272 | index = -(index+1); |
| 273 | } |
| 274 | mAllAppsList.add(index, item); |
| 275 | } |
| 276 | mAppsAdapter.notifyDataSetChanged(); |
| 277 | } |
| 278 | |
| 279 | public void removeApps(ArrayList<ApplicationInfo> list) { |
| 280 | final int N = list.size(); |
| 281 | for (int i=0; i<N; i++) { |
| 282 | final ApplicationInfo item = list.get(i); |
| 283 | int index = findAppByComponent(mAllAppsList, item); |
| 284 | if (index >= 0) { |
| 285 | mAllAppsList.remove(index); |
| 286 | } else { |
| 287 | Log.w(TAG, "couldn't find a match for item \"" + item + "\""); |
| 288 | // Try to recover. This should keep us from crashing for now. |
| 289 | } |
| 290 | } |
| 291 | mAppsAdapter.notifyDataSetChanged(); |
| 292 | } |
| 293 | |
| 294 | public void updateApps(String packageName, ArrayList<ApplicationInfo> list) { |
| 295 | // Just remove and add, because they may need to be re-sorted. |
| 296 | removeApps(list); |
| 297 | addApps(list); |
| 298 | } |
| 299 | |
| 300 | private static int findAppByComponent(ArrayList<ApplicationInfo> list, ApplicationInfo item) { |
| 301 | ComponentName component = item.intent.getComponent(); |
| 302 | final int N = list.size(); |
| 303 | for (int i=0; i<N; i++) { |
| 304 | ApplicationInfo x = list.get(i); |
| 305 | if (x.intent.getComponent().equals(component)) { |
| 306 | return i; |
| 307 | } |
| 308 | } |
| 309 | return -1; |
| 310 | } |
| 311 | |
| 312 | public void dumpState() { |
| 313 | ApplicationInfo.dumpApplicationInfoList(TAG, "mAllAppsList", mAllAppsList); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | |