| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import android.animation.AnimatorInflater; |
| 20 |
import android.animation.ObjectAnimator; |
| 21 |
import android.content.Context; |
| 22 |
import android.content.Intent; |
| 23 |
import android.content.res.TypedArray; |
| 24 |
import android.os.Handler; |
| 25 |
import android.os.Parcel; |
| 26 |
import android.os.Parcelable; |
| 27 |
import android.util.AttributeSet; |
| 28 |
import android.view.MotionEvent; |
| 29 |
import android.view.View; |
| 30 |
import android.view.ViewConfiguration; |
| 31 |
import android.view.ViewGroup; |
| 32 |
import android.widget.RemoteViews.InteractionHandler; |
| 33 |
|
| 34 |
import java.util.ArrayList; |
| 35 |
import java.util.HashMap; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public abstract class AdapterViewAnimator extends AdapterView<Adapter> |
| 47 |
implements RemoteViewsAdapter.RemoteAdapterConnectionCallback, Advanceable { |
| 48 |
private static final String TAG = "RemoteViewAnimator"; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
int mWhichChild = 0; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
private int mRestoreWhichChild = -1; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
boolean mAnimateFirstTime = true; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
int mActiveOffset = 0; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
int mMaxNumActiveViews = 1; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
HashMap<Integer, ViewAndMetaData> mViewsMap = new HashMap<Integer, ViewAndMetaData>(); |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
ArrayList<Integer> mPreviousViews; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
int mCurrentWindowStart = 0; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
int mCurrentWindowEnd = -1; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
int mCurrentWindowStartUnbounded = 0; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
AdapterDataSetObserver mDataSetObserver; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
Adapter mAdapter; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
RemoteViewsAdapter mRemoteViewsAdapter; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
boolean mDeferNotifyDataSetChanged = false; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
boolean mFirstTime = true; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
boolean mLoopViews = true; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
int mReferenceChildWidth = -1; |
| 141 |
int mReferenceChildHeight = -1; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
ObjectAnimator mInAnimation; |
| 147 |
ObjectAnimator mOutAnimation; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
private int mTouchMode = TOUCH_MODE_NONE; |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
static final int TOUCH_MODE_NONE = 0; |
| 158 |
static final int TOUCH_MODE_DOWN_IN_CURRENT_VIEW = 1; |
| 159 |
static final int TOUCH_MODE_HANDLED = 2; |
| 160 |
|
| 161 |
private Runnable mPendingCheckForTap; |
| 162 |
|
| 163 |
private static final int DEFAULT_ANIMATION_DURATION = 200; |
| 164 |
|
| 165 |
public AdapterViewAnimator(Context context) { |
| 166 |
this(context, null); |
| 167 |
} |
| 168 |
|
| 169 |
public AdapterViewAnimator(Context context, AttributeSet attrs) { |
| 170 |
this(context, attrs, 0); |
| 171 |
} |
| 172 |
|
| 173 |
public AdapterViewAnimator(Context context, AttributeSet attrs, int defStyleAttr) { |
| 174 |
this(context, attrs, defStyleAttr, 0); |
| 175 |
} |
| 176 |
|
| 177 |
public AdapterViewAnimator( |
| 178 |
Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 179 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 180 |
|
| 181 |
final TypedArray a = context.obtainStyledAttributes(attrs, |
| 182 |
com.android.internal.R.styleable.AdapterViewAnimator, defStyleAttr, defStyleRes); |
| 183 |
saveAttributeDataForStyleable(context, com.android.internal.R.styleable.AdapterViewAnimator, |
| 184 |
attrs, a, defStyleAttr, defStyleRes); |
| 185 |
|
| 186 |
int resource = a.getResourceId( |
| 187 |
com.android.internal.R.styleable.AdapterViewAnimator_inAnimation, 0); |
| 188 |
if (resource > 0) { |
| 189 |
setInAnimation(context, resource); |
| 190 |
} else { |
| 191 |
setInAnimation(getDefaultInAnimation()); |
| 192 |
} |
| 193 |
|
| 194 |
resource = a.getResourceId(com.android.internal.R.styleable.AdapterViewAnimator_outAnimation, 0); |
| 195 |
if (resource > 0) { |
| 196 |
setOutAnimation(context, resource); |
| 197 |
} else { |
| 198 |
setOutAnimation(getDefaultOutAnimation()); |
| 199 |
} |
| 200 |
|
| 201 |
boolean flag = a.getBoolean( |
| 202 |
com.android.internal.R.styleable.AdapterViewAnimator_animateFirstView, true); |
| 203 |
setAnimateFirstView(flag); |
| 204 |
|
| 205 |
mLoopViews = a.getBoolean( |
| 206 |
com.android.internal.R.styleable.AdapterViewAnimator_loopViews, false); |
| 207 |
|
| 208 |
a.recycle(); |
| 209 |
|
| 210 |
initViewAnimator(); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
private void initViewAnimator() { |
| 217 |
mPreviousViews = new ArrayList<Integer>(); |
| 218 |
} |
| 219 |
|
| 220 |
class ViewAndMetaData { |
| 221 |
View view; |
| 222 |
int relativeIndex; |
| 223 |
int adapterPosition; |
| 224 |
long itemId; |
| 225 |
|
| 226 |
ViewAndMetaData(View view, int relativeIndex, int adapterPosition, long itemId) { |
| 227 |
this.view = view; |
| 228 |
this.relativeIndex = relativeIndex; |
| 229 |
this.adapterPosition = adapterPosition; |
| 230 |
this.itemId = itemId; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
void configureViewAnimator(int numVisibleViews, int activeOffset) { |
| 248 |
if (activeOffset > numVisibleViews - 1) { |
| 249 |
|
| 250 |
} |
| 251 |
mMaxNumActiveViews = numVisibleViews; |
| 252 |
mActiveOffset = activeOffset; |
| 253 |
mPreviousViews.clear(); |
| 254 |
mViewsMap.clear(); |
| 255 |
removeAllViewsInLayout(); |
| 256 |
mCurrentWindowStart = 0; |
| 257 |
mCurrentWindowEnd = -1; |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
void transformViewForTransition(int fromIndex, int toIndex, View view, boolean animate) { |
| 271 |
if (fromIndex == -1) { |
| 272 |
mInAnimation.setTarget(view); |
| 273 |
mInAnimation.start(); |
| 274 |
} else if (toIndex == -1) { |
| 275 |
mOutAnimation.setTarget(view); |
| 276 |
mOutAnimation.start(); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
ObjectAnimator getDefaultInAnimation() { |
| 281 |
ObjectAnimator anim = ObjectAnimator.ofFloat(null, "alpha", 0.0f, 1.0f); |
| 282 |
anim.setDuration(DEFAULT_ANIMATION_DURATION); |
| 283 |
return anim; |
| 284 |
} |
| 285 |
|
| 286 |
ObjectAnimator getDefaultOutAnimation() { |
| 287 |
ObjectAnimator anim = ObjectAnimator.ofFloat(null, "alpha", 1.0f, 0.0f); |
| 288 |
anim.setDuration(DEFAULT_ANIMATION_DURATION); |
| 289 |
return anim; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
@android.view.RemotableViewMethod |
| 298 |
public void setDisplayedChild(int whichChild) { |
| 299 |
setDisplayedChild(whichChild, true); |
| 300 |
} |
| 301 |
|
| 302 |
private void setDisplayedChild(int whichChild, boolean animate) { |
| 303 |
if (mAdapter != null) { |
| 304 |
mWhichChild = whichChild; |
| 305 |
if (whichChild >= getWindowSize()) { |
| 306 |
mWhichChild = mLoopViews ? 0 : getWindowSize() - 1; |
| 307 |
} else if (whichChild < 0) { |
| 308 |
mWhichChild = mLoopViews ? getWindowSize() - 1 : 0; |
| 309 |
} |
| 310 |
|
| 311 |
boolean hasFocus = getFocusedChild() != null; |
| 312 |
|
| 313 |
showOnly(mWhichChild, animate); |
| 314 |
if (hasFocus) { |
| 315 |
|
| 316 |
requestFocus(FOCUS_FORWARD); |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
void applyTransformForChildAtIndex(View child, int relativeIndex) { |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
public int getDisplayedChild() { |
| 335 |
return mWhichChild; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
public void showNext() { |
| 342 |
setDisplayedChild(mWhichChild + 1); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
public void showPrevious() { |
| 349 |
setDisplayedChild(mWhichChild - 1); |
| 350 |
} |
| 351 |
|
| 352 |
int modulo(int pos, int size) { |
| 353 |
if (size > 0) { |
| 354 |
return (size + (pos % size)) % size; |
| 355 |
} else { |
| 356 |
return 0; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
View getViewAtRelativeIndex(int relativeIndex) { |
| 367 |
if (relativeIndex >= 0 && relativeIndex <= getNumActiveViews() - 1 && mAdapter != null) { |
| 368 |
int i = modulo(mCurrentWindowStartUnbounded + relativeIndex, getWindowSize()); |
| 369 |
if (mViewsMap.get(i) != null) { |
| 370 |
return mViewsMap.get(i).view; |
| 371 |
} |
| 372 |
} |
| 373 |
return null; |
| 374 |
} |
| 375 |
|
| 376 |
int getNumActiveViews() { |
| 377 |
if (mAdapter != null) { |
| 378 |
return Math.min(getCount() + 1, mMaxNumActiveViews); |
| 379 |
} else { |
| 380 |
return mMaxNumActiveViews; |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
int getWindowSize() { |
| 385 |
if (mAdapter != null) { |
| 386 |
int adapterCount = getCount(); |
| 387 |
if (adapterCount <= getNumActiveViews() && mLoopViews) { |
| 388 |
return adapterCount*mMaxNumActiveViews; |
| 389 |
} else { |
| 390 |
return adapterCount; |
| 391 |
} |
| 392 |
} else { |
| 393 |
return 0; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
private ViewAndMetaData getMetaDataForChild(View child) { |
| 398 |
for (ViewAndMetaData vm: mViewsMap.values()) { |
| 399 |
if (vm.view == child) { |
| 400 |
return vm; |
| 401 |
} |
| 402 |
} |
| 403 |
return null; |
| 404 |
} |
| 405 |
|
| 406 |
LayoutParams createOrReuseLayoutParams(View v) { |
| 407 |
final LayoutParams currentLp = v.getLayoutParams(); |
| 408 |
if (currentLp != null) { |
| 409 |
return currentLp; |
| 410 |
} |
| 411 |
return new LayoutParams(0, 0); |
| 412 |
} |
| 413 |
|
| 414 |
void refreshChildren() { |
| 415 |
final int adapterCount = mAdapter == null ? 0 : getCount(); |
| 416 |
for (int i = mCurrentWindowStart; i <= mCurrentWindowEnd; i++) { |
| 417 |
int index = modulo(i, getWindowSize()); |
| 418 |
|
| 419 |
final View updatedChild; |
| 420 |
if (i < adapterCount) { |
| 421 |
|
| 422 |
updatedChild = mAdapter.getView(modulo(i, adapterCount), null, this); |
| 423 |
|
| 424 |
if (updatedChild.getImportantForAccessibility() |
| 425 |
== IMPORTANT_FOR_ACCESSIBILITY_AUTO) { |
| 426 |
updatedChild.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES); |
| 427 |
} |
| 428 |
} else { |
| 429 |
updatedChild = null; |
| 430 |
} |
| 431 |
|
| 432 |
if (mViewsMap.containsKey(index)) { |
| 433 |
final FrameLayout fl = (FrameLayout) mViewsMap.get(index).view; |
| 434 |
|
| 435 |
fl.removeAllViewsInLayout(); |
| 436 |
if (updatedChild != null) { |
| 437 |
|
| 438 |
fl.addView(updatedChild); |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
FrameLayout getFrameForChild() { |
| 452 |
return new FrameLayout(mContext); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
void showOnly(int childIndex, boolean animate) { |
| 466 |
if (mAdapter == null) return; |
| 467 |
final int adapterCount = getCount(); |
| 468 |
if (adapterCount == 0) return; |
| 469 |
|
| 470 |
for (int i = 0; i < mPreviousViews.size(); i++) { |
| 471 |
View viewToRemove = mViewsMap.get(mPreviousViews.get(i)).view; |
| 472 |
mViewsMap.remove(mPreviousViews.get(i)); |
| 473 |
viewToRemove.clearAnimation(); |
| 474 |
if (viewToRemove instanceof ViewGroup) { |
| 475 |
ViewGroup vg = (ViewGroup) viewToRemove; |
| 476 |
vg.removeAllViewsInLayout(); |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
applyTransformForChildAtIndex(viewToRemove, -1); |
| 481 |
|
| 482 |
removeViewInLayout(viewToRemove); |
| 483 |
} |
| 484 |
mPreviousViews.clear(); |
| 485 |
int newWindowStartUnbounded = childIndex - mActiveOffset; |
| 486 |
int newWindowEndUnbounded = newWindowStartUnbounded + getNumActiveViews() - 1; |
| 487 |
int newWindowStart = Math.max(0, newWindowStartUnbounded); |
| 488 |
int newWindowEnd = Math.min(adapterCount - 1, newWindowEndUnbounded); |
| 489 |
|
| 490 |
if (mLoopViews) { |
| 491 |
newWindowStart = newWindowStartUnbounded; |
| 492 |
newWindowEnd = newWindowEndUnbounded; |
| 493 |
} |
| 494 |
int rangeStart = modulo(newWindowStart, getWindowSize()); |
| 495 |
int rangeEnd = modulo(newWindowEnd, getWindowSize()); |
| 496 |
|
| 497 |
boolean wrap = false; |
| 498 |
if (rangeStart > rangeEnd) { |
| 499 |
wrap = true; |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
for (Integer index : mViewsMap.keySet()) { |
| 507 |
boolean remove = false; |
| 508 |
if (!wrap && (index < rangeStart || index > rangeEnd)) { |
| 509 |
remove = true; |
| 510 |
} else if (wrap && (index > rangeEnd && index < rangeStart)) { |
| 511 |
remove = true; |
| 512 |
} |
| 513 |
|
| 514 |
if (remove) { |
| 515 |
View previousView = mViewsMap.get(index).view; |
| 516 |
int oldRelativeIndex = mViewsMap.get(index).relativeIndex; |
| 517 |
|
| 518 |
mPreviousViews.add(index); |
| 519 |
transformViewForTransition(oldRelativeIndex, -1, previousView, animate); |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
if (!(newWindowStart == mCurrentWindowStart && newWindowEnd == mCurrentWindowEnd && |
| 525 |
newWindowStartUnbounded == mCurrentWindowStartUnbounded)) { |
| 526 |
|
| 527 |
for (int i = newWindowStart; i <= newWindowEnd; i++) { |
| 528 |
|
| 529 |
int index = modulo(i, getWindowSize()); |
| 530 |
int oldRelativeIndex; |
| 531 |
if (mViewsMap.containsKey(index)) { |
| 532 |
oldRelativeIndex = mViewsMap.get(index).relativeIndex; |
| 533 |
} else { |
| 534 |
oldRelativeIndex = -1; |
| 535 |
} |
| 536 |
int newRelativeIndex = i - newWindowStartUnbounded; |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
boolean inOldRange = mViewsMap.containsKey(index) && !mPreviousViews.contains(index); |
| 542 |
|
| 543 |
if (inOldRange) { |
| 544 |
View view = mViewsMap.get(index).view; |
| 545 |
mViewsMap.get(index).relativeIndex = newRelativeIndex; |
| 546 |
applyTransformForChildAtIndex(view, newRelativeIndex); |
| 547 |
transformViewForTransition(oldRelativeIndex, newRelativeIndex, view, animate); |
| 548 |
|
| 549 |
|
| 550 |
} else { |
| 551 |
|
| 552 |
final int adapterPosition = modulo(i, adapterCount); |
| 553 |
View newView = mAdapter.getView(adapterPosition, null, this); |
| 554 |
long itemId = mAdapter.getItemId(adapterPosition); |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
FrameLayout fl = getFrameForChild(); |
| 559 |
|
| 560 |
|
| 561 |
if (newView != null) { |
| 562 |
fl.addView(newView); |
| 563 |
} |
| 564 |
mViewsMap.put(index, new ViewAndMetaData(fl, newRelativeIndex, |
| 565 |
adapterPosition, itemId)); |
| 566 |
addChild(fl); |
| 567 |
applyTransformForChildAtIndex(fl, newRelativeIndex); |
| 568 |
transformViewForTransition(-1, newRelativeIndex, fl, animate); |
| 569 |
} |
| 570 |
mViewsMap.get(index).view.bringToFront(); |
| 571 |
} |
| 572 |
mCurrentWindowStart = newWindowStart; |
| 573 |
mCurrentWindowEnd = newWindowEnd; |
| 574 |
mCurrentWindowStartUnbounded = newWindowStartUnbounded; |
| 575 |
if (mRemoteViewsAdapter != null) { |
| 576 |
int adapterStart = modulo(mCurrentWindowStart, adapterCount); |
| 577 |
int adapterEnd = modulo(mCurrentWindowEnd, adapterCount); |
| 578 |
mRemoteViewsAdapter.setVisibleRangeHint(adapterStart, adapterEnd); |
| 579 |
} |
| 580 |
} |
| 581 |
requestLayout(); |
| 582 |
invalidate(); |
| 583 |
} |
| 584 |
|
| 585 |
private void addChild(View child) { |
| 586 |
addViewInLayout(child, -1, createOrReuseLayoutParams(child)); |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
if (mReferenceChildWidth == -1 || mReferenceChildHeight == -1) { |
| 592 |
int measureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); |
| 593 |
child.measure(measureSpec, measureSpec); |
| 594 |
mReferenceChildWidth = child.getMeasuredWidth(); |
| 595 |
mReferenceChildHeight = child.getMeasuredHeight(); |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
void showTapFeedback(View v) { |
| 600 |
v.setPressed(true); |
| 601 |
} |
| 602 |
|
| 603 |
void hideTapFeedback(View v) { |
| 604 |
v.setPressed(false); |
| 605 |
} |
| 606 |
|
| 607 |
void cancelHandleClick() { |
| 608 |
View v = getCurrentView(); |
| 609 |
if (v != null) { |
| 610 |
hideTapFeedback(v); |
| 611 |
} |
| 612 |
mTouchMode = TOUCH_MODE_NONE; |
| 613 |
} |
| 614 |
|
| 615 |
final class CheckForTap implements Runnable { |
| 616 |
public void run() { |
| 617 |
if (mTouchMode == TOUCH_MODE_DOWN_IN_CURRENT_VIEW) { |
| 618 |
View v = getCurrentView(); |
| 619 |
showTapFeedback(v); |
| 620 |
} |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
@Override |
| 625 |
public boolean onTouchEvent(MotionEvent ev) { |
| 626 |
int action = ev.getAction(); |
| 627 |
boolean handled = false; |
| 628 |
switch (action) { |
| 629 |
case MotionEvent.ACTION_DOWN: { |
| 630 |
View v = getCurrentView(); |
| 631 |
if (v != null) { |
| 632 |
if (isTransformedTouchPointInView(ev.getX(), ev.getY(), v, null)) { |
| 633 |
if (mPendingCheckForTap == null) { |
| 634 |
mPendingCheckForTap = new CheckForTap(); |
| 635 |
} |
| 636 |
mTouchMode = TOUCH_MODE_DOWN_IN_CURRENT_VIEW; |
| 637 |
postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout()); |
| 638 |
} |
| 639 |
} |
| 640 |
break; |
| 641 |
} |
| 642 |
case MotionEvent.ACTION_MOVE: break; |
| 643 |
case MotionEvent.ACTION_POINTER_UP: break; |
| 644 |
case MotionEvent.ACTION_UP: { |
| 645 |
if (mTouchMode == TOUCH_MODE_DOWN_IN_CURRENT_VIEW) { |
| 646 |
final View v = getCurrentView(); |
| 647 |
final ViewAndMetaData viewData = getMetaDataForChild(v); |
| 648 |
if (v != null) { |
| 649 |
if (isTransformedTouchPointInView(ev.getX(), ev.getY(), v, null)) { |
| 650 |
final Handler handler = getHandler(); |
| 651 |
if (handler != null) { |
| 652 |
handler.removeCallbacks(mPendingCheckForTap); |
| 653 |
} |
| 654 |
showTapFeedback(v); |
| 655 |
postDelayed(new Runnable() { |
| 656 |
public void run() { |
| 657 |
hideTapFeedback(v); |
| 658 |
post(new Runnable() { |
| 659 |
public void run() { |
| 660 |
if (viewData != null) { |
| 661 |
performItemClick(v, viewData.adapterPosition, |
| 662 |
viewData.itemId); |
| 663 |
} else { |
| 664 |
performItemClick(v, 0, 0); |
| 665 |
} |
| 666 |
} |
| 667 |
}); |
| 668 |
} |
| 669 |
}, ViewConfiguration.getPressedStateDuration()); |
| 670 |
handled = true; |
| 671 |
} |
| 672 |
} |
| 673 |
} |
| 674 |
mTouchMode = TOUCH_MODE_NONE; |
| 675 |
break; |
| 676 |
} |
| 677 |
case MotionEvent.ACTION_CANCEL: { |
| 678 |
View v = getCurrentView(); |
| 679 |
if (v != null) { |
| 680 |
hideTapFeedback(v); |
| 681 |
} |
| 682 |
mTouchMode = TOUCH_MODE_NONE; |
| 683 |
} |
| 684 |
} |
| 685 |
return handled; |
| 686 |
} |
| 687 |
|
| 688 |
private void measureChildren() { |
| 689 |
final int count = getChildCount(); |
| 690 |
final int childWidth = getMeasuredWidth() - mPaddingLeft - mPaddingRight; |
| 691 |
final int childHeight = getMeasuredHeight() - mPaddingTop - mPaddingBottom; |
| 692 |
|
| 693 |
for (int i = 0; i < count; i++) { |
| 694 |
final View child = getChildAt(i); |
| 695 |
child.measure(MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY), |
| 696 |
MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY)); |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
@Override |
| 701 |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 702 |
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); |
| 703 |
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); |
| 704 |
final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); |
| 705 |
final int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); |
| 706 |
|
| 707 |
boolean haveChildRefSize = (mReferenceChildWidth != -1 && mReferenceChildHeight != -1); |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
if (heightSpecMode == MeasureSpec.UNSPECIFIED) { |
| 713 |
heightSpecSize = haveChildRefSize ? mReferenceChildHeight + mPaddingTop + |
| 714 |
mPaddingBottom : 0; |
| 715 |
} else if (heightSpecMode == MeasureSpec.AT_MOST) { |
| 716 |
if (haveChildRefSize) { |
| 717 |
int height = mReferenceChildHeight + mPaddingTop + mPaddingBottom; |
| 718 |
if (height > heightSpecSize) { |
| 719 |
heightSpecSize |= MEASURED_STATE_TOO_SMALL; |
| 720 |
} else { |
| 721 |
heightSpecSize = height; |
| 722 |
} |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
if (widthSpecMode == MeasureSpec.UNSPECIFIED) { |
| 727 |
widthSpecSize = haveChildRefSize ? mReferenceChildWidth + mPaddingLeft + |
| 728 |
mPaddingRight : 0; |
| 729 |
} else if (heightSpecMode == MeasureSpec.AT_MOST) { |
| 730 |
if (haveChildRefSize) { |
| 731 |
int width = mReferenceChildWidth + mPaddingLeft + mPaddingRight; |
| 732 |
if (width > widthSpecSize) { |
| 733 |
widthSpecSize |= MEASURED_STATE_TOO_SMALL; |
| 734 |
} else { |
| 735 |
widthSpecSize = width; |
| 736 |
} |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
setMeasuredDimension(widthSpecSize, heightSpecSize); |
| 741 |
measureChildren(); |
| 742 |
} |
| 743 |
|
| 744 |
void checkForAndHandleDataChanged() { |
| 745 |
boolean dataChanged = mDataChanged; |
| 746 |
if (dataChanged) { |
| 747 |
post(new Runnable() { |
| 748 |
public void run() { |
| 749 |
handleDataChanged(); |
| 750 |
|
| 751 |
|
| 752 |
if (mWhichChild >= getWindowSize()) { |
| 753 |
mWhichChild = 0; |
| 754 |
|
| 755 |
showOnly(mWhichChild, false); |
| 756 |
} else if (mOldItemCount != getCount()) { |
| 757 |
showOnly(mWhichChild, false); |
| 758 |
} |
| 759 |
refreshChildren(); |
| 760 |
requestLayout(); |
| 761 |
} |
| 762 |
}); |
| 763 |
} |
| 764 |
mDataChanged = false; |
| 765 |
} |
| 766 |
|
| 767 |
@Override |
| 768 |
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 769 |
checkForAndHandleDataChanged(); |
| 770 |
|
| 771 |
final int childCount = getChildCount(); |
| 772 |
for (int i = 0; i < childCount; i++) { |
| 773 |
final View child = getChildAt(i); |
| 774 |
|
| 775 |
int childRight = mPaddingLeft + child.getMeasuredWidth(); |
| 776 |
int childBottom = mPaddingTop + child.getMeasuredHeight(); |
| 777 |
|
| 778 |
child.layout(mPaddingLeft, mPaddingTop, childRight, childBottom); |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
static class SavedState extends BaseSavedState { |
| 783 |
int whichChild; |
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
SavedState(Parcelable superState, int whichChild) { |
| 789 |
super(superState); |
| 790 |
this.whichChild = whichChild; |
| 791 |
} |
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
private SavedState(Parcel in) { |
| 797 |
super(in); |
| 798 |
this.whichChild = in.readInt(); |
| 799 |
} |
| 800 |
|
| 801 |
@Override |
| 802 |
public void writeToParcel(Parcel out, int flags) { |
| 803 |
super.writeToParcel(out, flags); |
| 804 |
out.writeInt(this.whichChild); |
| 805 |
} |
| 806 |
|
| 807 |
@Override |
| 808 |
public String toString() { |
| 809 |
return "AdapterViewAnimator.SavedState{ whichChild = " + this.whichChild + " }"; |
| 810 |
} |
| 811 |
|
| 812 |
public static final @android.annotation.NonNull Parcelable.Creator<SavedState> CREATOR |
| 813 |
= new Parcelable.Creator<SavedState>() { |
| 814 |
public SavedState createFromParcel(Parcel in) { |
| 815 |
return new SavedState(in); |
| 816 |
} |
| 817 |
|
| 818 |
public SavedState[] newArray(int size) { |
| 819 |
return new SavedState[size]; |
| 820 |
} |
| 821 |
}; |
| 822 |
} |
| 823 |
|
| 824 |
@Override |
| 825 |
public Parcelable onSaveInstanceState() { |
| 826 |
Parcelable superState = super.onSaveInstanceState(); |
| 827 |
if (mRemoteViewsAdapter != null) { |
| 828 |
mRemoteViewsAdapter.saveRemoteViewsCache(); |
| 829 |
} |
| 830 |
return new SavedState(superState, mWhichChild); |
| 831 |
} |
| 832 |
|
| 833 |
@Override |
| 834 |
public void onRestoreInstanceState(Parcelable state) { |
| 835 |
SavedState ss = (SavedState) state; |
| 836 |
super.onRestoreInstanceState(ss.getSuperState()); |
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
mWhichChild = ss.whichChild; |
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
if (mRemoteViewsAdapter != null && mAdapter == null) { |
| 848 |
mRestoreWhichChild = mWhichChild; |
| 849 |
} else { |
| 850 |
setDisplayedChild(mWhichChild, false); |
| 851 |
} |
| 852 |
} |
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
public View getCurrentView() { |
| 862 |
return getViewAtRelativeIndex(mActiveOffset); |
| 863 |
} |
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
public ObjectAnimator getInAnimation() { |
| 874 |
return mInAnimation; |
| 875 |
} |
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
public void setInAnimation(ObjectAnimator inAnimation) { |
| 886 |
mInAnimation = inAnimation; |
| 887 |
} |
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
public ObjectAnimator getOutAnimation() { |
| 898 |
return mOutAnimation; |
| 899 |
} |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
public void setOutAnimation(ObjectAnimator outAnimation) { |
| 910 |
mOutAnimation = outAnimation; |
| 911 |
} |
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
public void setInAnimation(Context context, int resourceID) { |
| 923 |
setInAnimation((ObjectAnimator) AnimatorInflater.loadAnimator(context, resourceID)); |
| 924 |
} |
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
public void setOutAnimation(Context context, int resourceID) { |
| 936 |
setOutAnimation((ObjectAnimator) AnimatorInflater.loadAnimator(context, resourceID)); |
| 937 |
} |
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
public void setAnimateFirstView(boolean animate) { |
| 947 |
mAnimateFirstTime = animate; |
| 948 |
} |
| 949 |
|
| 950 |
@Override |
| 951 |
public int getBaseline() { |
| 952 |
return (getCurrentView() != null) ? getCurrentView().getBaseline() : super.getBaseline(); |
| 953 |
} |
| 954 |
|
| 955 |
@Override |
| 956 |
public Adapter getAdapter() { |
| 957 |
return mAdapter; |
| 958 |
} |
| 959 |
|
| 960 |
@Override |
| 961 |
public void setAdapter(Adapter adapter) { |
| 962 |
if (mAdapter != null && mDataSetObserver != null) { |
| 963 |
mAdapter.unregisterDataSetObserver(mDataSetObserver); |
| 964 |
} |
| 965 |
|
| 966 |
mAdapter = adapter; |
| 967 |
checkFocus(); |
| 968 |
|
| 969 |
if (mAdapter != null) { |
| 970 |
mDataSetObserver = new AdapterDataSetObserver(); |
| 971 |
mAdapter.registerDataSetObserver(mDataSetObserver); |
| 972 |
mItemCount = mAdapter.getCount(); |
| 973 |
} |
| 974 |
setFocusable(true); |
| 975 |
mWhichChild = 0; |
| 976 |
showOnly(mWhichChild, false); |
| 977 |
} |
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
@android.view.RemotableViewMethod(asyncImpl="setRemoteViewsAdapterAsync") |
| 987 |
public void setRemoteViewsAdapter(Intent intent) { |
| 988 |
setRemoteViewsAdapter(intent, false); |
| 989 |
} |
| 990 |
|
| 991 |
|
| 992 |
public Runnable setRemoteViewsAdapterAsync(final Intent intent) { |
| 993 |
return new RemoteViewsAdapter.AsyncRemoteAdapterAction(this, intent); |
| 994 |
} |
| 995 |
|
| 996 |
|
| 997 |
@Override |
| 998 |
public void setRemoteViewsAdapter(Intent intent, boolean isAsync) { |
| 999 |
|
| 1000 |
|
| 1001 |
if (mRemoteViewsAdapter != null) { |
| 1002 |
Intent.FilterComparison fcNew = new Intent.FilterComparison(intent); |
| 1003 |
Intent.FilterComparison fcOld = new Intent.FilterComparison( |
| 1004 |
mRemoteViewsAdapter.getRemoteViewsServiceIntent()); |
| 1005 |
if (fcNew.equals(fcOld)) { |
| 1006 |
return; |
| 1007 |
} |
| 1008 |
} |
| 1009 |
mDeferNotifyDataSetChanged = false; |
| 1010 |
|
| 1011 |
mRemoteViewsAdapter = new RemoteViewsAdapter(getContext(), intent, this, isAsync); |
| 1012 |
if (mRemoteViewsAdapter.isDataReady()) { |
| 1013 |
setAdapter(mRemoteViewsAdapter); |
| 1014 |
} |
| 1015 |
} |
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
public void setRemoteViewsOnClickHandler(InteractionHandler handler) { |
| 1025 |
|
| 1026 |
|
| 1027 |
if (mRemoteViewsAdapter != null) { |
| 1028 |
mRemoteViewsAdapter.setRemoteViewsInteractionHandler(handler); |
| 1029 |
} |
| 1030 |
} |
| 1031 |
|
| 1032 |
@Override |
| 1033 |
public void setSelection(int position) { |
| 1034 |
setDisplayedChild(position); |
| 1035 |
} |
| 1036 |
|
| 1037 |
@Override |
| 1038 |
public View getSelectedView() { |
| 1039 |
return getViewAtRelativeIndex(mActiveOffset); |
| 1040 |
} |
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
public void deferNotifyDataSetChanged() { |
| 1047 |
mDeferNotifyDataSetChanged = true; |
| 1048 |
} |
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
public boolean onRemoteAdapterConnected() { |
| 1054 |
if (mRemoteViewsAdapter != mAdapter) { |
| 1055 |
setAdapter(mRemoteViewsAdapter); |
| 1056 |
|
| 1057 |
if (mDeferNotifyDataSetChanged) { |
| 1058 |
mRemoteViewsAdapter.notifyDataSetChanged(); |
| 1059 |
mDeferNotifyDataSetChanged = false; |
| 1060 |
} |
| 1061 |
|
| 1062 |
|
| 1063 |
if (mRestoreWhichChild > -1) { |
| 1064 |
setDisplayedChild(mRestoreWhichChild, false); |
| 1065 |
mRestoreWhichChild = -1; |
| 1066 |
} |
| 1067 |
return false; |
| 1068 |
} else if (mRemoteViewsAdapter != null) { |
| 1069 |
mRemoteViewsAdapter.superNotifyDataSetChanged(); |
| 1070 |
return true; |
| 1071 |
} |
| 1072 |
return false; |
| 1073 |
} |
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
public void onRemoteAdapterDisconnected() { |
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
} |
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
public void advance() { |
| 1091 |
showNext(); |
| 1092 |
} |
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
public void fyiWillBeAdvancedByHostKThx() { |
| 1101 |
} |
| 1102 |
|
| 1103 |
@Override |
| 1104 |
public CharSequence getAccessibilityClassName() { |
| 1105 |
return AdapterViewAnimator.class.getName(); |
| 1106 |
} |
| 1107 |
} |