@@ -127,14 +127,55 @@ public class ActivityOptions {
127127 */
128128 public static ActivityOptions makeCustomAnimation (Context context ,
129129 int enterResId , int exitResId ) {
130+ return makeCustomAnimation (context , enterResId , exitResId , null , null );
131+ }
132+
133+ /**
134+ * Create an ActivityOptions specifying a custom animation to run when
135+ * the activity is displayed.
136+ *
137+ * @param context Who is defining this. This is the application that the
138+ * animation resources will be loaded from.
139+ * @param enterResId A resource ID of the animation resource to use for
140+ * the incoming activity. Use 0 for no animation.
141+ * @param exitResId A resource ID of the animation resource to use for
142+ * the outgoing activity. Use 0 for no animation.
143+ * @param handler If <var>listener</var> is non-null this must be a valid
144+ * Handler on which to dispatch the callback; otherwise it should be null.
145+ * @param listener Optional OnAnimationStartedListener to find out when the
146+ * requested animation has started running. If for some reason the animation
147+ * is not executed, the callback will happen immediately.
148+ * @return Returns a new ActivityOptions object that you can use to
149+ * supply these options as the options Bundle when starting an activity.
150+ * @hide
151+ */
152+ public static ActivityOptions makeCustomAnimation (Context context ,
153+ int enterResId , int exitResId , Handler handler , OnAnimationStartedListener listener ) {
130154 ActivityOptions opts = new ActivityOptions ();
131155 opts .mPackageName = context .getPackageName ();
132156 opts .mAnimationType = ANIM_CUSTOM ;
133157 opts .mCustomEnterResId = enterResId ;
134158 opts .mCustomExitResId = exitResId ;
159+ opts .setListener (handler , listener );
135160 return opts ;
136161 }
137162
163+ private void setListener (Handler handler , OnAnimationStartedListener listener ) {
164+ if (listener != null ) {
165+ final Handler h = handler ;
166+ final OnAnimationStartedListener finalListener = listener ;
167+ mAnimationStartedListener = new IRemoteCallback .Stub () {
168+ @ Override public void sendResult (Bundle data ) throws RemoteException {
169+ h .post (new Runnable () {
170+ @ Override public void run () {
171+ finalListener .onAnimationStarted ();
172+ }
173+ });
174+ }
175+ };
176+ }
177+ }
178+
138179 /**
139180 * Callback for use with {@link ActivityOptions#makeThumbnailScaleUpAnimation}
140181 * to find out when the given animation has started running.
@@ -258,19 +299,7 @@ private static ActivityOptions makeThumbnailScaleUpAnimation(View source,
258299 source .getLocationOnScreen (pts );
259300 opts .mStartX = pts [0 ] + startX ;
260301 opts .mStartY = pts [1 ] + startY ;
261- if (listener != null ) {
262- final Handler h = source .getHandler ();
263- final OnAnimationStartedListener finalListener = listener ;
264- opts .mAnimationStartedListener = new IRemoteCallback .Stub () {
265- @ Override public void sendResult (Bundle data ) throws RemoteException {
266- h .post (new Runnable () {
267- @ Override public void run () {
268- finalListener .onAnimationStarted ();
269- }
270- });
271- }
272- };
273- }
302+ opts .setListener (source .getHandler (), listener );
274303 return opts ;
275304 }
276305
@@ -284,6 +313,8 @@ public ActivityOptions(Bundle opts) {
284313 if (mAnimationType == ANIM_CUSTOM ) {
285314 mCustomEnterResId = opts .getInt (KEY_ANIM_ENTER_RES_ID , 0 );
286315 mCustomExitResId = opts .getInt (KEY_ANIM_EXIT_RES_ID , 0 );
316+ mAnimationStartedListener = IRemoteCallback .Stub .asInterface (
317+ opts .getIBinder (KEY_ANIM_START_LISTENER ));
287318 } else if (mAnimationType == ANIM_SCALE_UP ) {
288319 mStartX = opts .getInt (KEY_ANIM_START_X , 0 );
289320 mStartY = opts .getInt (KEY_ANIM_START_Y , 0 );
@@ -381,14 +412,27 @@ public void update(ActivityOptions otherOptions) {
381412 mCustomEnterResId = otherOptions .mCustomEnterResId ;
382413 mCustomExitResId = otherOptions .mCustomExitResId ;
383414 mThumbnail = null ;
384- mAnimationStartedListener = null ;
415+ if (otherOptions .mAnimationStartedListener != null ) {
416+ try {
417+ otherOptions .mAnimationStartedListener .sendResult (null );
418+ } catch (RemoteException e ) {
419+ }
420+ }
421+ mAnimationStartedListener = otherOptions .mAnimationStartedListener ;
385422 break ;
386423 case ANIM_SCALE_UP :
387424 mAnimationType = otherOptions .mAnimationType ;
388425 mStartX = otherOptions .mStartX ;
389426 mStartY = otherOptions .mStartY ;
390427 mStartWidth = otherOptions .mStartWidth ;
391428 mStartHeight = otherOptions .mStartHeight ;
429+ if (otherOptions .mAnimationStartedListener != null ) {
430+ try {
431+ otherOptions .mAnimationStartedListener .sendResult (null );
432+ } catch (RemoteException e ) {
433+ }
434+ }
435+ mAnimationStartedListener = null ;
392436 break ;
393437 case ANIM_THUMBNAIL :
394438 case ANIM_THUMBNAIL_DELAYED :
@@ -425,6 +469,8 @@ public Bundle toBundle() {
425469 b .putInt (KEY_ANIM_TYPE , mAnimationType );
426470 b .putInt (KEY_ANIM_ENTER_RES_ID , mCustomEnterResId );
427471 b .putInt (KEY_ANIM_EXIT_RES_ID , mCustomExitResId );
472+ b .putIBinder (KEY_ANIM_START_LISTENER , mAnimationStartedListener
473+ != null ? mAnimationStartedListener .asBinder () : null );
428474 break ;
429475 case ANIM_SCALE_UP :
430476 b .putInt (KEY_ANIM_TYPE , mAnimationType );
0 commit comments