AnimationThread.java revision 2eea6fab1cbb0a5c8f913491c2d622c904759893
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 17package com.android.layoutlib.bridge.impl; 18 19import com.android.layoutlib.api.SceneResult; 20import com.android.layoutlib.api.LayoutScene.IAnimationListener; 21 22import android.animation.Animator; 23import android.animation.ValueAnimator; 24import android.os.Handler; 25import android.os.Handler_Delegate; 26import android.os.Message; 27import android.os.Handler_Delegate.IHandlerCallback; 28 29import java.util.LinkedList; 30import java.util.Queue; 31 32public class AnimationThread extends Thread { 33 34 private static class MessageBundle { 35 final Handler mTarget; 36 final Message mMessage; 37 final long mUptimeMillis; 38 39 MessageBundle(Handler target, Message message, long uptimeMillis) { 40 mTarget = target; 41 mMessage = message; 42 mUptimeMillis = uptimeMillis; 43 } 44 } 45 46 private final LayoutSceneImpl mScene; 47 private final Animator mAnimator; 48 49 Queue<MessageBundle> mQueue = new LinkedList<MessageBundle>(); 50 private final IAnimationListener mListener; 51 52 public AnimationThread(LayoutSceneImpl scene, Animator animator, IAnimationListener listener) { 53 mScene = scene; 54 mAnimator = animator; 55 mListener = listener; 56 } 57 58 @Override 59 public void run() { 60 mScene.prepareThread(); 61 try { 62 Handler_Delegate.setCallback(new IHandlerCallback() { 63 public void sendMessageAtTime(Handler handler, Message msg, long uptimeMillis) { 64 if (msg.what == ValueAnimator.ANIMATION_START || 65 msg.what == ValueAnimator.ANIMATION_FRAME) { 66 mQueue.add(new MessageBundle(handler, msg, uptimeMillis)); 67 } else { 68 // just ignore. 69 } 70 } 71 }); 72 73 // start the animation. This will send a message to the handler right away, so 74 // mQueue is filled when this method returns. 75 mAnimator.start(); 76 77 // loop the animation 78 do { 79 // get the next message. 80 MessageBundle bundle = mQueue.poll(); 81 if (bundle == null) { 82 break; 83 } 84 85 // sleep enough for this bundle to be on time 86 long currentTime = System.currentTimeMillis(); 87 if (currentTime < bundle.mUptimeMillis) { 88 try { 89 sleep(bundle.mUptimeMillis - currentTime); 90 } catch (InterruptedException e) { 91 // TODO Auto-generated catch block 92 e.printStackTrace(); 93 } 94 } 95 96 // ready to do the work, acquire the scene. 97 SceneResult result = mScene.acquire(250); 98 if (result != SceneResult.SUCCESS) { 99 mListener.done(result); 100 return; 101 } 102 103 // process the bundle. If the animation is not finished, this will enqueue 104 // the next message, so mQueue will have another one. 105 try { 106 bundle.mTarget.handleMessage(bundle.mMessage); 107 if (mScene.render() == SceneResult.SUCCESS) { 108 mListener.onNewFrame(mScene.getImage()); 109 } 110 } finally { 111 mScene.release(); 112 } 113 } while (mQueue.size() > 0); 114 115 mListener.done(SceneResult.SUCCESS); 116 } finally { 117 Handler_Delegate.setCallback(null); 118 mScene.cleanupThread(); 119 } 120 } 121} 122