Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.statusbar.tablet; |
| 18 | |
| 19 | import java.util.ArrayList; |
| 20 | import java.util.List; |
| 21 | |
Jim Miller | e5c16ab | 2010-12-03 19:18:38 -0800 | [diff] [blame] | 22 | import android.animation.Animator; |
Jim Miller | e5c16ab | 2010-12-03 19:18:38 -0800 | [diff] [blame] | 23 | import android.animation.AnimatorSet; |
Jim Miller | e5c16ab | 2010-12-03 19:18:38 -0800 | [diff] [blame] | 24 | import android.animation.ObjectAnimator; |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 25 | import android.app.ActivityManager; |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 28 | import android.content.pm.ActivityInfo; |
| 29 | import android.content.pm.PackageManager; |
| 30 | import android.content.pm.ResolveInfo; |
Jim Miller | e5c16ab | 2010-12-03 19:18:38 -0800 | [diff] [blame] | 31 | import android.content.res.Configuration; |
| 32 | import android.content.res.Resources; |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 33 | import android.graphics.Bitmap; |
Jim Miller | f9905b4 | 2010-12-22 20:37:17 -0800 | [diff] [blame] | 34 | import android.graphics.BitmapFactory; |
Jim Miller | e5c16ab | 2010-12-03 19:18:38 -0800 | [diff] [blame] | 35 | import android.graphics.Canvas; |
Jim Miller | fefef31 | 2010-11-09 14:08:39 -0800 | [diff] [blame] | 36 | import android.graphics.Matrix; |
Jim Miller | e5c16ab | 2010-12-03 19:18:38 -0800 | [diff] [blame] | 37 | import android.graphics.Paint; |
| 38 | import android.graphics.Rect; |
Jim Miller | f9905b4 | 2010-12-22 20:37:17 -0800 | [diff] [blame] | 39 | import android.graphics.RectF; |
Jim Miller | e5c16ab | 2010-12-03 19:18:38 -0800 | [diff] [blame] | 40 | import android.graphics.Shader.TileMode; |
| 41 | import android.graphics.drawable.BitmapDrawable; |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 42 | import android.graphics.drawable.Drawable; |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 43 | import android.util.AttributeSet; |
Jim Miller | e5c16ab | 2010-12-03 19:18:38 -0800 | [diff] [blame] | 44 | import android.util.DisplayMetrics; |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 45 | import android.util.Log; |
Jim Miller | 85babff | 2011-01-11 14:26:03 -0800 | [diff] [blame] | 46 | import android.util.Slog; |
Jim Miller | 5fda69c | 2011-02-16 15:17:11 -0800 | [diff] [blame] | 47 | import android.view.LayoutInflater; |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 48 | import android.view.View; |
Jim Miller | 5fda69c | 2011-02-16 15:17:11 -0800 | [diff] [blame] | 49 | import android.view.ViewGroup; |
| 50 | import android.widget.AdapterView; |
| 51 | import android.widget.AdapterView.OnItemClickListener; |
| 52 | import android.widget.BaseAdapter; |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 53 | import android.widget.CheckBox; |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 54 | import android.widget.ImageView; |
Jim Miller | 5fda69c | 2011-02-16 15:17:11 -0800 | [diff] [blame] | 55 | import android.widget.ListView; |
Jim Miller | 85babff | 2011-01-11 14:26:03 -0800 | [diff] [blame] | 56 | import android.widget.RelativeLayout; |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 57 | import android.widget.TextView; |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 58 | |
| 59 | import com.android.systemui.R; |
| 60 | |
Jim Miller | 5fda69c | 2011-02-16 15:17:11 -0800 | [diff] [blame] | 61 | public class RecentAppsPanel extends RelativeLayout implements StatusBarPanel, OnItemClickListener { |
Jim Miller | f9905b4 | 2010-12-22 20:37:17 -0800 | [diff] [blame] | 62 | private static final int GLOW_PADDING = 15; |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 63 | private static final String TAG = "RecentAppsPanel"; |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 64 | private static final boolean DEBUG = TabletStatusBar.DEBUG; |
Jim Miller | 5fda69c | 2011-02-16 15:17:11 -0800 | [diff] [blame] | 65 | private static final int DISPLAY_TASKS = 20; |
| 66 | private static final int MAX_TASKS = DISPLAY_TASKS + 1; // allow extra for non-apps |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 67 | private TabletStatusBar mBar; |
Jim Miller | fefef31 | 2010-11-09 14:08:39 -0800 | [diff] [blame] | 68 | private ArrayList<ActivityDescription> mActivityDescriptions; |
Jim Miller | e5c16ab | 2010-12-03 19:18:38 -0800 | [diff] [blame] | 69 | private int mIconDpi; |
Jim Miller | 85babff | 2011-01-11 14:26:03 -0800 | [diff] [blame] | 70 | private View mRecentsScrim; |
| 71 | private View mRecentsGlowView; |
Jim Miller | 5fda69c | 2011-02-16 15:17:11 -0800 | [diff] [blame] | 72 | private ListView mRecentsContainer; |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 73 | private CheckBox mCompatMode; |
Jim Miller | f9905b4 | 2010-12-22 20:37:17 -0800 | [diff] [blame] | 74 | private Bitmap mGlowBitmap; |
Jim Miller | 85babff | 2011-01-11 14:26:03 -0800 | [diff] [blame] | 75 | private boolean mShowing; |
| 76 | private Choreographer mChoreo; |
Jim Miller | f1e22e5 | 2011-01-26 15:39:51 -0800 | [diff] [blame] | 77 | private View mRecentsDismissButton; |
Jim Miller | 5fda69c | 2011-02-16 15:17:11 -0800 | [diff] [blame] | 78 | private ActvityDescriptionAdapter mListAdapter; |
| 79 | protected int mLastVisibleItem; |
Jim Miller | fefef31 | 2010-11-09 14:08:39 -0800 | [diff] [blame] | 80 | |
| 81 | static class ActivityDescription { |
| 82 | int id; |
| 83 | Bitmap thumbnail; // generated by Activity.onCreateThumbnail() |
| 84 | Drawable icon; // application package icon |
| 85 | String label; // application package label |
| 86 | CharSequence description; // generated by Activity.onCreateDescription() |
| 87 | Intent intent; // launch intent for application |
| 88 | Matrix matrix; // arbitrary rotation matrix to correct orientation |
Jim Miller | e5c16ab | 2010-12-03 19:18:38 -0800 | [diff] [blame] | 89 | String packageName; // used to override animations (see onClick()) |
Jim Miller | fefef31 | 2010-11-09 14:08:39 -0800 | [diff] [blame] | 90 | int position; // position in list |
| 91 | |
| 92 | public ActivityDescription(Bitmap _thumbnail, |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 93 | Drawable _icon, String _label, CharSequence _desc, Intent _intent, |
| 94 | int _id, int _pos, String _packageName) |
Jim Miller | fefef31 | 2010-11-09 14:08:39 -0800 | [diff] [blame] | 95 | { |
| 96 | thumbnail = _thumbnail; |
| 97 | icon = _icon; |
| 98 | label = _label; |
| 99 | description = _desc; |
| 100 | intent = _intent; |
| 101 | id = _id; |
| 102 | position = _pos; |
Jim Miller | e5c16ab | 2010-12-03 19:18:38 -0800 | [diff] [blame] | 103 | packageName = _packageName; |
Jim Miller | fefef31 | 2010-11-09 14:08:39 -0800 | [diff] [blame] | 104 | } |
| 105 | }; |
| 106 | |
Jim Miller | b5a2b29 | 2011-03-01 16:14:59 -0800 | [diff] [blame] | 107 | /* package */ final static class ViewHolder { |
Jim Miller | 5fda69c | 2011-02-16 15:17:11 -0800 | [diff] [blame] | 108 | private ImageView thumbnailView; |
| 109 | private ImageView iconView; |
| 110 | private TextView labelView; |
| 111 | private TextView descriptionView; |
| 112 | private ActivityDescription activityDescription; |
| 113 | } |
| 114 | |
Jim Miller | b5a2b29 | 2011-03-01 16:14:59 -0800 | [diff] [blame] | 115 | /* package */ final class ActvityDescriptionAdapter extends BaseAdapter { |
Jim Miller | 5fda69c | 2011-02-16 15:17:11 -0800 | [diff] [blame] | 116 | private LayoutInflater mInflater; |
| 117 | |
| 118 | public ActvityDescriptionAdapter(Context context) { |
| 119 | mInflater = LayoutInflater.from(context); |
| 120 | } |
| 121 | |
| 122 | public int getCount() { |
| 123 | return mActivityDescriptions != null ? mActivityDescriptions.size() : 0; |
| 124 | } |
| 125 | |
| 126 | public Object getItem(int position) { |
| 127 | return position; // we only need the index |
| 128 | } |
| 129 | |
| 130 | public long getItemId(int position) { |
| 131 | return position; // we just need something unique for this position |
| 132 | } |
| 133 | |
| 134 | public View getView(int position, View convertView, ViewGroup parent) { |
| 135 | ViewHolder holder; |
| 136 | if (convertView == null) { |
| 137 | convertView = mInflater.inflate(R.layout.status_bar_recent_item, null); |
| 138 | holder = new ViewHolder(); |
| 139 | holder.thumbnailView = (ImageView) convertView.findViewById(R.id.app_thumbnail); |
| 140 | holder.iconView = (ImageView) convertView.findViewById(R.id.app_icon); |
| 141 | holder.labelView = (TextView) convertView.findViewById(R.id.app_label); |
| 142 | holder.descriptionView = (TextView) convertView.findViewById(R.id.app_description); |
| 143 | convertView.setTag(holder); |
| 144 | } else { |
| 145 | holder = (ViewHolder) convertView.getTag(); |
| 146 | } |
| 147 | |
| 148 | // activityId is reverse since most recent appears at the bottom... |
| 149 | final int activityId = mActivityDescriptions.size() - position - 1; |
| 150 | |
| 151 | final ActivityDescription activityDescription = mActivityDescriptions.get(activityId); |
| 152 | final Bitmap thumb = activityDescription.thumbnail; |
| 153 | holder.thumbnailView.setImageBitmap(compositeBitmap(mGlowBitmap, thumb)); |
| 154 | holder.iconView.setImageDrawable(activityDescription.icon); |
| 155 | holder.labelView.setText(activityDescription.label); |
| 156 | holder.descriptionView.setText(activityDescription.description); |
| 157 | holder.thumbnailView.setTag(activityDescription); |
| 158 | holder.activityDescription = activityDescription; |
| 159 | |
| 160 | return convertView; |
| 161 | } |
| 162 | } |
| 163 | |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 164 | public boolean isInContentArea(int x, int y) { |
Jim Miller | 6f5bd3c | 2011-01-25 17:03:27 -0800 | [diff] [blame] | 165 | // use mRecentsContainer's exact bounds to determine horizontal position |
| 166 | final int l = mRecentsContainer.getLeft(); |
| 167 | final int r = mRecentsContainer.getRight(); |
| 168 | // use surrounding mRecentsGlowView's position in parent determine vertical bounds |
| 169 | final int t = mRecentsGlowView.getTop(); |
| 170 | final int b = mRecentsGlowView.getBottom(); |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 171 | return x >= l && x < r && y >= t && y < b; |
| 172 | } |
| 173 | |
Jim Miller | 85babff | 2011-01-11 14:26:03 -0800 | [diff] [blame] | 174 | public void show(boolean show, boolean animate) { |
| 175 | if (animate) { |
| 176 | if (mShowing != show) { |
| 177 | mShowing = show; |
| 178 | if (show) { |
| 179 | setVisibility(View.VISIBLE); |
| 180 | } |
| 181 | mChoreo.startAnimation(show); |
| 182 | } |
| 183 | } else { |
| 184 | mShowing = show; |
| 185 | setVisibility(show ? View.VISIBLE : View.GONE); |
| 186 | mChoreo.jumpTo(show); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * We need to be aligned at the bottom. LinearLayout can't do this, so instead, |
| 192 | * let LinearLayout do all the hard work, and then shift everything down to the bottom. |
| 193 | */ |
| 194 | @Override |
| 195 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 196 | super.onLayout(changed, l, t, r, b); |
| 197 | mChoreo.setPanelHeight(mRecentsContainer.getHeight()); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Whether the panel is showing, or, if it's animating, whether it will be |
| 202 | * when the animation is done. |
| 203 | */ |
| 204 | public boolean isShowing() { |
| 205 | return mShowing; |
| 206 | } |
| 207 | |
| 208 | private static class Choreographer implements Animator.AnimatorListener { |
| 209 | // should group this into a multi-property animation |
| 210 | private static final int OPEN_DURATION = 136; |
| 211 | private static final int CLOSE_DURATION = 250; |
| 212 | |
| 213 | boolean mVisible; |
| 214 | int mPanelHeight; |
| 215 | View mRootView; |
| 216 | View mScrimView; |
| 217 | View mContentView; |
| 218 | AnimatorSet mContentAnim; |
| 219 | |
| 220 | // the panel will start to appear this many px from the end |
| 221 | final int HYPERSPACE_OFFRAMP = 200; |
| 222 | |
| 223 | public Choreographer(View root, View scrim, View content) { |
| 224 | mRootView = root; |
| 225 | mScrimView = scrim; |
| 226 | mContentView = content; |
| 227 | } |
| 228 | |
| 229 | void createAnimation(boolean appearing) { |
| 230 | float start, end; |
| 231 | |
| 232 | if (DEBUG) Log.e(TAG, "createAnimation()", new Exception()); |
| 233 | |
| 234 | // 0: on-screen |
| 235 | // height: off-screen |
| 236 | float y = mContentView.getTranslationY(); |
| 237 | if (appearing) { |
| 238 | // we want to go from near-the-top to the top, unless we're half-open in the right |
| 239 | // general vicinity |
| 240 | start = (y < HYPERSPACE_OFFRAMP) ? y : HYPERSPACE_OFFRAMP; |
| 241 | end = 0; |
| 242 | } else { |
| 243 | start = y; |
| 244 | end = y + HYPERSPACE_OFFRAMP; |
| 245 | } |
| 246 | |
| 247 | Animator posAnim = ObjectAnimator.ofFloat(mContentView, "translationY", |
| 248 | start, end); |
| 249 | posAnim.setInterpolator(appearing |
| 250 | ? new android.view.animation.DecelerateInterpolator(2.5f) |
| 251 | : new android.view.animation.AccelerateInterpolator(2.5f)); |
| 252 | |
| 253 | Animator glowAnim = ObjectAnimator.ofFloat(mContentView, "alpha", |
| 254 | mContentView.getAlpha(), appearing ? 1.0f : 0.0f); |
| 255 | glowAnim.setInterpolator(appearing |
| 256 | ? new android.view.animation.AccelerateInterpolator(1.0f) |
| 257 | : new android.view.animation.DecelerateInterpolator(1.0f)); |
| 258 | |
| 259 | Animator bgAnim = ObjectAnimator.ofInt(mScrimView.getBackground(), |
| 260 | "alpha", appearing ? 0 : 255, appearing ? 255 : 0); |
| 261 | |
| 262 | mContentAnim = new AnimatorSet(); |
| 263 | mContentAnim |
| 264 | .play(bgAnim) |
| 265 | .with(glowAnim) |
| 266 | .with(posAnim); |
| 267 | mContentAnim.setDuration(appearing ? OPEN_DURATION : CLOSE_DURATION); |
| 268 | mContentAnim.addListener(this); |
| 269 | } |
| 270 | |
| 271 | void startAnimation(boolean appearing) { |
| 272 | if (DEBUG) Slog.d(TAG, "startAnimation(appearing=" + appearing + ")"); |
| 273 | |
| 274 | createAnimation(appearing); |
| 275 | |
| 276 | mContentView.setLayerType(View.LAYER_TYPE_HARDWARE, null); |
| 277 | mContentAnim.start(); |
| 278 | |
| 279 | mVisible = appearing; |
| 280 | } |
| 281 | |
| 282 | void jumpTo(boolean appearing) { |
| 283 | mContentView.setTranslationY(appearing ? 0 : mPanelHeight); |
| 284 | } |
| 285 | |
| 286 | public void setPanelHeight(int h) { |
| 287 | if (DEBUG) Slog.d(TAG, "panelHeight=" + h); |
| 288 | mPanelHeight = h; |
| 289 | } |
| 290 | |
| 291 | public void onAnimationCancel(Animator animation) { |
| 292 | if (DEBUG) Slog.d(TAG, "onAnimationCancel"); |
| 293 | // force this to zero so we close the window |
| 294 | mVisible = false; |
| 295 | } |
| 296 | |
| 297 | public void onAnimationEnd(Animator animation) { |
| 298 | if (DEBUG) Slog.d(TAG, "onAnimationEnd"); |
| 299 | if (!mVisible) { |
| 300 | mRootView.setVisibility(View.GONE); |
| 301 | } |
| 302 | mContentView.setLayerType(View.LAYER_TYPE_NONE, null); |
| 303 | mContentAnim = null; |
| 304 | } |
| 305 | |
| 306 | public void onAnimationRepeat(Animator animation) { |
| 307 | } |
| 308 | |
| 309 | public void onAnimationStart(Animator animation) { |
| 310 | } |
| 311 | } |
| 312 | |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 313 | public void setBar(TabletStatusBar bar) { |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 314 | mBar = bar; |
| 315 | } |
| 316 | |
| 317 | public RecentAppsPanel(Context context, AttributeSet attrs) { |
| 318 | this(context, attrs, 0); |
| 319 | } |
| 320 | |
| 321 | public RecentAppsPanel(Context context, AttributeSet attrs, int defStyle) { |
| 322 | super(context, attrs, defStyle); |
Jim Miller | e5c16ab | 2010-12-03 19:18:38 -0800 | [diff] [blame] | 323 | |
| 324 | Resources res = context.getResources(); |
| 325 | boolean xlarge = (res.getConfiguration().screenLayout |
| 326 | & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE; |
| 327 | |
| 328 | mIconDpi = xlarge ? DisplayMetrics.DENSITY_HIGH : res.getDisplayMetrics().densityDpi; |
Jim Miller | f9905b4 | 2010-12-22 20:37:17 -0800 | [diff] [blame] | 329 | mGlowBitmap = BitmapFactory.decodeResource(res, R.drawable.recents_thumbnail_bg); |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | @Override |
Jim Miller | 5fda69c | 2011-02-16 15:17:11 -0800 | [diff] [blame] | 333 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 334 | super.onSizeChanged(w, h, oldw, oldh); |
| 335 | // Keep track of the last visible item in the list so we can restore it |
| 336 | // to the bottom when the orientation changes. |
| 337 | int childCount = mRecentsContainer.getChildCount(); |
| 338 | if (childCount > 0) { |
| 339 | mLastVisibleItem = mRecentsContainer.getFirstVisiblePosition() + childCount - 1; |
| 340 | View view = mRecentsContainer.getChildAt(childCount - 1); |
| 341 | final int distanceFromBottom = mRecentsContainer.getHeight() - view.getTop(); |
| 342 | //final int distanceFromBottom = view.getHeight() + BOTTOM_OFFSET; |
| 343 | |
| 344 | // This has to happen post-layout, so run it "in the future" |
| 345 | post(new Runnable() { |
| 346 | public void run() { |
| 347 | mRecentsContainer.setSelectionFromTop(mLastVisibleItem, |
| 348 | mRecentsContainer.getHeight() - distanceFromBottom); |
| 349 | } |
| 350 | }); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | @Override |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 355 | protected void onFinishInflate() { |
| 356 | super.onFinishInflate(); |
Jim Miller | 5fda69c | 2011-02-16 15:17:11 -0800 | [diff] [blame] | 357 | LayoutInflater inflater = (LayoutInflater) |
| 358 | mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 359 | |
| 360 | mRecentsContainer = (ListView) findViewById(R.id.recents_container); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 361 | mCompatMode = (CheckBox) findViewById(R.id.recents_compat_mode); |
| 362 | mCompatMode.setOnClickListener(new OnClickListener() { |
| 363 | public void onClick(View v) { |
| 364 | final ActivityManager am = (ActivityManager) |
| 365 | mContext.getSystemService(Context.ACTIVITY_SERVICE); |
| 366 | am.setFrontActivityScreenCompatMode(ActivityManager.COMPAT_MODE_TOGGLE); |
| 367 | hide(true); |
| 368 | } |
| 369 | }); |
Jim Miller | 5fda69c | 2011-02-16 15:17:11 -0800 | [diff] [blame] | 370 | View footer = inflater.inflate(R.layout.status_bar_recent_panel_footer, |
| 371 | mRecentsContainer, false); |
| 372 | mRecentsContainer.setScrollbarFadingEnabled(true); |
Jim Miller | b5a2b29 | 2011-03-01 16:14:59 -0800 | [diff] [blame] | 373 | mRecentsContainer.addFooterView(footer, null, false); |
Jim Miller | 5fda69c | 2011-02-16 15:17:11 -0800 | [diff] [blame] | 374 | mRecentsContainer.setAdapter(mListAdapter = new ActvityDescriptionAdapter(mContext)); |
| 375 | mRecentsContainer.setOnItemClickListener(this); |
Jim Miller | 67edceb | 2011-03-02 18:32:16 -0800 | [diff] [blame] | 376 | final int leftPadding = mContext.getResources() |
| 377 | .getDimensionPixelOffset(R.dimen.status_bar_recents_thumbnail_left_margin); |
| 378 | mRecentsContainer.setOverScrollEffectPadding(leftPadding, 0); |
Jim Miller | 5fda69c | 2011-02-16 15:17:11 -0800 | [diff] [blame] | 379 | |
Jim Miller | 169a211 | 2010-12-10 18:30:07 -0800 | [diff] [blame] | 380 | mRecentsGlowView = findViewById(R.id.recents_glow); |
Jim Miller | 85babff | 2011-01-11 14:26:03 -0800 | [diff] [blame] | 381 | mRecentsScrim = (View) findViewById(R.id.recents_bg_protect); |
| 382 | mChoreo = new Choreographer(this, mRecentsScrim, mRecentsGlowView); |
Jim Miller | f1e22e5 | 2011-01-26 15:39:51 -0800 | [diff] [blame] | 383 | mRecentsDismissButton = findViewById(R.id.recents_dismiss_button); |
| 384 | mRecentsDismissButton.setOnClickListener(new OnClickListener() { |
| 385 | public void onClick(View v) { |
| 386 | hide(true); |
| 387 | } |
| 388 | }); |
Jim Miller | e5c16ab | 2010-12-03 19:18:38 -0800 | [diff] [blame] | 389 | |
| 390 | // In order to save space, we make the background texture repeat in the Y direction |
Jim Miller | 85babff | 2011-01-11 14:26:03 -0800 | [diff] [blame] | 391 | if (mRecentsScrim != null && mRecentsScrim.getBackground() instanceof BitmapDrawable) { |
| 392 | ((BitmapDrawable) mRecentsScrim.getBackground()).setTileModeY(TileMode.REPEAT); |
Jim Miller | e5c16ab | 2010-12-03 19:18:38 -0800 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
| 396 | @Override |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 397 | protected void onVisibilityChanged(View changedView, int visibility) { |
| 398 | super.onVisibilityChanged(changedView, visibility); |
Jim Miller | e5c16ab | 2010-12-03 19:18:38 -0800 | [diff] [blame] | 399 | if (DEBUG) Log.v(TAG, "onVisibilityChanged(" + changedView + ", " + visibility + ")"); |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 400 | if (visibility == View.VISIBLE && changedView == this) { |
Jim Miller | fefef31 | 2010-11-09 14:08:39 -0800 | [diff] [blame] | 401 | refreshApplicationList(); |
Jim Miller | 5fda69c | 2011-02-16 15:17:11 -0800 | [diff] [blame] | 402 | post(new Runnable() { |
| 403 | public void run() { |
| 404 | mRecentsContainer.setSelection(mActivityDescriptions.size() - 1); |
| 405 | } |
| 406 | }); |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
Jim Miller | e5c16ab | 2010-12-03 19:18:38 -0800 | [diff] [blame] | 410 | private Drawable getFullResDefaultActivityIcon() { |
| 411 | return getFullResIcon(Resources.getSystem(), |
Michael Jurka | 573068c | 2011-01-17 13:29:53 -0800 | [diff] [blame] | 412 | com.android.internal.R.mipmap.sym_def_app_icon); |
Jim Miller | e5c16ab | 2010-12-03 19:18:38 -0800 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | private Drawable getFullResIcon(Resources resources, int iconId) { |
| 416 | return resources.getDrawableForDensity(iconId, mIconDpi); |
| 417 | } |
| 418 | |
| 419 | private Drawable getFullResIcon(ResolveInfo info, PackageManager packageManager) { |
| 420 | Resources resources; |
| 421 | try { |
| 422 | resources = packageManager.getResourcesForApplication( |
| 423 | info.activityInfo.applicationInfo); |
| 424 | } catch (PackageManager.NameNotFoundException e) { |
| 425 | resources = null; |
| 426 | } |
| 427 | if (resources != null) { |
| 428 | int iconId = info.activityInfo.getIconResource(); |
| 429 | if (iconId != 0) { |
| 430 | return getFullResIcon(resources, iconId); |
| 431 | } |
| 432 | } |
| 433 | return getFullResDefaultActivityIcon(); |
| 434 | } |
| 435 | |
Jim Miller | fefef31 | 2010-11-09 14:08:39 -0800 | [diff] [blame] | 436 | private ArrayList<ActivityDescription> getRecentTasks() { |
| 437 | ArrayList<ActivityDescription> activityDescriptions = new ArrayList<ActivityDescription>(); |
| 438 | final PackageManager pm = mContext.getPackageManager(); |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 439 | final ActivityManager am = (ActivityManager) |
Jim Miller | fefef31 | 2010-11-09 14:08:39 -0800 | [diff] [blame] | 440 | mContext.getSystemService(Context.ACTIVITY_SERVICE); |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 441 | |
Jim Miller | fefef31 | 2010-11-09 14:08:39 -0800 | [diff] [blame] | 442 | final List<ActivityManager.RecentTaskInfo> recentTasks = |
Dianne Hackborn | d283593 | 2010-12-13 16:28:46 -0800 | [diff] [blame] | 443 | am.getRecentTasks(MAX_TASKS, ActivityManager.RECENT_IGNORE_UNAVAILABLE |
| 444 | | ActivityManager.TASKS_GET_THUMBNAILS); |
Jim Miller | fefef31 | 2010-11-09 14:08:39 -0800 | [diff] [blame] | 445 | |
| 446 | ActivityInfo homeInfo = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME) |
| 447 | .resolveActivityInfo(pm, 0); |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 448 | |
| 449 | int numTasks = recentTasks.size(); |
Jim Miller | d19bede | 2010-12-14 17:17:05 -0800 | [diff] [blame] | 450 | |
| 451 | // skip the first activity - assume it's either the home screen or the current app. |
| 452 | final int first = 1; |
| 453 | for (int i = first, index = 0; i < numTasks && (index < MAX_TASKS); ++i) { |
Jim Miller | fefef31 | 2010-11-09 14:08:39 -0800 | [diff] [blame] | 454 | final ActivityManager.RecentTaskInfo recentInfo = recentTasks.get(i); |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 455 | |
Jim Miller | fefef31 | 2010-11-09 14:08:39 -0800 | [diff] [blame] | 456 | Intent intent = new Intent(recentInfo.baseIntent); |
| 457 | if (recentInfo.origActivity != null) { |
| 458 | intent.setComponent(recentInfo.origActivity); |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Jim Miller | fefef31 | 2010-11-09 14:08:39 -0800 | [diff] [blame] | 461 | // Skip the current home activity. |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 462 | if (homeInfo != null |
| 463 | && homeInfo.packageName.equals(intent.getComponent().getPackageName()) |
| 464 | && homeInfo.name.equals(intent.getComponent().getClassName())) { |
Jim Miller | fefef31 | 2010-11-09 14:08:39 -0800 | [diff] [blame] | 465 | continue; |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | intent.setFlags((intent.getFlags()&~Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) |
| 469 | | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 470 | final ResolveInfo resolveInfo = pm.resolveActivity(intent, 0); |
| 471 | if (resolveInfo != null) { |
Jim Miller | fefef31 | 2010-11-09 14:08:39 -0800 | [diff] [blame] | 472 | final ActivityInfo info = resolveInfo.activityInfo; |
| 473 | final String title = info.loadLabel(pm).toString(); |
Jim Miller | e5c16ab | 2010-12-03 19:18:38 -0800 | [diff] [blame] | 474 | // Drawable icon = info.loadIcon(pm); |
| 475 | Drawable icon = getFullResIcon(resolveInfo, pm); |
Jim Miller | fefef31 | 2010-11-09 14:08:39 -0800 | [diff] [blame] | 476 | int id = recentTasks.get(i).id; |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 477 | if (title != null && title.length() > 0 && icon != null) { |
Jim Miller | e5c16ab | 2010-12-03 19:18:38 -0800 | [diff] [blame] | 478 | if (DEBUG) Log.v(TAG, "creating activity desc for id=" + id + ", label=" + title); |
Jim Miller | fefef31 | 2010-11-09 14:08:39 -0800 | [diff] [blame] | 479 | ActivityDescription item = new ActivityDescription( |
Dianne Hackborn | d94df45 | 2011-02-16 18:53:31 -0800 | [diff] [blame] | 480 | am.getTaskThumbnail(recentInfo.persistentId), |
| 481 | icon, title, recentInfo.description, intent, id, |
| 482 | index, info.packageName); |
Jim Miller | fefef31 | 2010-11-09 14:08:39 -0800 | [diff] [blame] | 483 | activityDescriptions.add(item); |
| 484 | ++index; |
| 485 | } else { |
Jim Miller | e5c16ab | 2010-12-03 19:18:38 -0800 | [diff] [blame] | 486 | if (DEBUG) Log.v(TAG, "SKIPPING item " + id); |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | } |
Jim Miller | fefef31 | 2010-11-09 14:08:39 -0800 | [diff] [blame] | 490 | return activityDescriptions; |
| 491 | } |
| 492 | |
| 493 | ActivityDescription findActivityDescription(int id) |
| 494 | { |
| 495 | ActivityDescription desc = null; |
| 496 | for (int i = 0; i < mActivityDescriptions.size(); i++) { |
| 497 | ActivityDescription item = mActivityDescriptions.get(i); |
| 498 | if (item != null && item.id == id) { |
| 499 | desc = item; |
| 500 | break; |
| 501 | } |
| 502 | } |
| 503 | return desc; |
| 504 | } |
| 505 | |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 506 | private void updateShownCompatMode() { |
| 507 | final ActivityManager am = (ActivityManager) |
| 508 | mContext.getSystemService(Context.ACTIVITY_SERVICE); |
| 509 | int mode = am.getFrontActivityScreenCompatMode(); |
| 510 | switch (mode) { |
| 511 | case ActivityManager.COMPAT_MODE_DISABLED: |
| 512 | mCompatMode.setVisibility(View.VISIBLE); |
| 513 | mCompatMode.setChecked(true); |
| 514 | break; |
| 515 | case ActivityManager.COMPAT_MODE_ENABLED: |
| 516 | mCompatMode.setVisibility(View.VISIBLE); |
| 517 | mCompatMode.setChecked(false); |
| 518 | break; |
| 519 | default: |
| 520 | mCompatMode.setVisibility(View.GONE); |
| 521 | break; |
| 522 | } |
| 523 | } |
| 524 | |
Jim Miller | fefef31 | 2010-11-09 14:08:39 -0800 | [diff] [blame] | 525 | private void refreshApplicationList() { |
| 526 | mActivityDescriptions = getRecentTasks(); |
Jim Miller | 5fda69c | 2011-02-16 15:17:11 -0800 | [diff] [blame] | 527 | mListAdapter.notifyDataSetInvalidated(); |
Jim Miller | 01f9944 | 2011-01-10 21:02:25 -0800 | [diff] [blame] | 528 | if (mActivityDescriptions.size() > 0) { |
Jim Miller | 5fda69c | 2011-02-16 15:17:11 -0800 | [diff] [blame] | 529 | mLastVisibleItem = mActivityDescriptions.size() - 1; // scroll to bottom after reloading |
| 530 | updateUiElements(getResources().getConfiguration()); |
Dianne Hackborn | 0f1de9a | 2011-05-11 17:34:49 -0700 | [diff] [blame] | 531 | updateShownCompatMode(); |
Jim Miller | 01f9944 | 2011-01-10 21:02:25 -0800 | [diff] [blame] | 532 | } else { |
Jim Miller | 85babff | 2011-01-11 14:26:03 -0800 | [diff] [blame] | 533 | // Immediately hide this panel |
Jim Miller | f1e22e5 | 2011-01-26 15:39:51 -0800 | [diff] [blame] | 534 | hide(false); |
Jim Miller | 01f9944 | 2011-01-10 21:02:25 -0800 | [diff] [blame] | 535 | } |
Jim Miller | fefef31 | 2010-11-09 14:08:39 -0800 | [diff] [blame] | 536 | } |
| 537 | |
Jim Miller | f9905b4 | 2010-12-22 20:37:17 -0800 | [diff] [blame] | 538 | private Bitmap compositeBitmap(Bitmap background, Bitmap thumbnail) { |
| 539 | Bitmap outBitmap = background.copy(background.getConfig(), true); |
Jim Miller | 25eef89 | 2011-01-04 13:52:22 -0800 | [diff] [blame] | 540 | if (thumbnail != null) { |
| 541 | Canvas canvas = new Canvas(outBitmap); |
| 542 | Paint paint = new Paint(); |
| 543 | paint.setAntiAlias(true); |
| 544 | paint.setFilterBitmap(true); |
| 545 | paint.setAlpha(255); |
| 546 | final int srcWidth = thumbnail.getWidth(); |
Jim Miller | e70d506 | 2011-03-08 21:38:39 -0800 | [diff] [blame] | 547 | final int srcHeight = thumbnail.getHeight(); |
Jim Miller | 25eef89 | 2011-01-04 13:52:22 -0800 | [diff] [blame] | 548 | canvas.drawBitmap(thumbnail, |
| 549 | new Rect(0, 0, srcWidth-1, srcHeight-1), |
| 550 | new RectF(GLOW_PADDING, |
Jim Miller | 5fda69c | 2011-02-16 15:17:11 -0800 | [diff] [blame] | 551 | GLOW_PADDING - 7.0f, |
| 552 | outBitmap.getWidth() - GLOW_PADDING + 3.0f, |
| 553 | outBitmap.getHeight() - GLOW_PADDING + 7.0f), paint); |
Jim Miller | 25eef89 | 2011-01-04 13:52:22 -0800 | [diff] [blame] | 554 | } |
Jim Miller | e5c16ab | 2010-12-03 19:18:38 -0800 | [diff] [blame] | 555 | return outBitmap; |
| 556 | } |
| 557 | |
Jim Miller | 5fda69c | 2011-02-16 15:17:11 -0800 | [diff] [blame] | 558 | private void updateUiElements(Configuration config) { |
| 559 | final int items = mActivityDescriptions.size(); |
Jim Miller | e5c16ab | 2010-12-03 19:18:38 -0800 | [diff] [blame] | 560 | |
Jim Miller | 5fda69c | 2011-02-16 15:17:11 -0800 | [diff] [blame] | 561 | mRecentsContainer.setVisibility(items > 0 ? View.VISIBLE : View.GONE); |
| 562 | mRecentsGlowView.setVisibility(items > 0 ? View.VISIBLE : View.GONE); |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Jim Miller | 5fda69c | 2011-02-16 15:17:11 -0800 | [diff] [blame] | 565 | private void hide(boolean animate) { |
| 566 | if (!animate) { |
| 567 | setVisibility(View.GONE); |
| 568 | } |
| 569 | mBar.animateCollapse(); |
| 570 | } |
| 571 | |
| 572 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
| 573 | ActivityDescription ad = ((ViewHolder) view.getTag()).activityDescription; |
Jim Miller | e5c16ab | 2010-12-03 19:18:38 -0800 | [diff] [blame] | 574 | final ActivityManager am = (ActivityManager) |
| 575 | getContext().getSystemService(Context.ACTIVITY_SERVICE); |
Dianne Hackborn | b677746 | 2010-11-12 12:43:22 -0800 | [diff] [blame] | 576 | if (ad.id >= 0) { |
| 577 | // This is an active task; it should just go to the foreground. |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 578 | am.moveTaskToFront(ad.id, ActivityManager.MOVE_TASK_WITH_HOME); |
Dianne Hackborn | b677746 | 2010-11-12 12:43:22 -0800 | [diff] [blame] | 579 | } else { |
| 580 | Intent intent = ad.intent; |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 581 | intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY |
| 582 | | Intent.FLAG_ACTIVITY_TASK_ON_HOME); |
Dianne Hackborn | b677746 | 2010-11-12 12:43:22 -0800 | [diff] [blame] | 583 | if (DEBUG) Log.v(TAG, "Starting activity " + intent); |
| 584 | getContext().startActivity(intent); |
| 585 | } |
Jim Miller | f1e22e5 | 2011-01-26 15:39:51 -0800 | [diff] [blame] | 586 | hide(true); |
| 587 | } |
Jim Miller | 44c66fe | 2010-10-20 18:32:52 -0700 | [diff] [blame] | 588 | } |